The maxlength attribute in the HTML <input> element specifies the maximum number of characters that the user can input into a text field. It limits the length of the input text to ensure that the data entered does not exceed a specific number of characters.
This attribute applies to input types such as text, password, search, email, tel, and url. If the user tries to enter more than the allowed characters, additional characters are not accepted by the field. The maxlength attribute doesn't restrict how much data is sent to the server, just what is entered in the UI.
<input type="text" maxlength="50">
In this example, the input field will only allow up to 50 characters.
maxlength.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Maxlength Example</title> </head> <body> <form action="https://httpbin.org/post" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username" maxlength="10" required> <input type="submit" value="Submit"> </form> </body> </html>
Above snippet generate below screen.
Try to enter more than 10 letters in the text fields to understand the behavior.
No comments:
Post a Comment