แอตทริบิวต์ HTML maxlength กำหนดจำนวนอักขระสูงสุดที่อนุญาตในองค์ประกอบ HTML อินพุตในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
<input maxlength=”text”>
ให้เรามาดูตัวอย่าง HTML maxlength Attribute −
ตัวอย่าง
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
background-color: #8BC6EC;
background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
text-align: center;
}
input {
width: 300px;
background: transparent;
border: 2px solid #fff;
outline: none;
display: block;
margin: 1rem auto;
border-radius: 20px;
padding: 8px 10px;
}
::placeholder {
color: #000;
}
.btn {
background: #db133a;
color: #fff;
border: none;
width: 320px;
}
</style>
<body>
<h1>HTML maxlength Attribute Demo</h1>
<form action="">
<input type="text" placeholder='Enter your first name(only 8 characters)' maxlength="8">
<input type="submit" value="SUBMIT" class="btn">
</form>
</body>
</html> ผลลัพธ์

ตอนนี้ให้ลองป้อนอักขระมากกว่า 8 ตัวในช่องป้อนข้อมูลเพื่อดูว่าแอตทริบิวต์ maxlength ทำงานอย่างไร -
