แอตทริบิวต์ maxlength ของ HTML ใช้กำหนดจำนวนตัวอักษรสูงสุดที่อนุญาตในองค์ประกอบ input ของ HTML ในเอกสาร HTML
ไวยากรณ์
ไวยากรณ์มีดังนี้ −
<input maxlength="text">
ลองดูตัวอย่างการใช้งานแอตทริบิวต์ maxlength ของ HTML −
ตัวอย่าง
<!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>การสาธิตแอตทริบิวต์ maxlength ของ HTML</h1> <form action=""> <input type="text" placeholder='ป้อนชื่อจริง (สูงสุด 8 ตัวอักษร)' maxlength="8"> <input type="submit" value="ส่ง" class="btn"> </form> </body> </html>
ผลลัพธ์
ลองป้อนมากกว่า 8 ตัวอักษรในช่องป้อนข้อมูลเพื่อสังเกตการทำงานของแอตทริบิวต์ maxlength −

