
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to limit the number of characters allowed in form input text field?
In this article, we will learn how to limit the number of characters allowed in form input text field.
We use <input> tag to get user input in HTML. To give a limit (or range) to the input field, we use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively.
To set the maximum character limit in input field, we use <input> maxlength attribute. This attribute is used to specify the maximum number of characters enters the <input> field.
To set the minimum character limit in input field, we use <input> minlength attribute. This attribute is used to specify the minimum number of characters enters the <input> field.
First, we see how to set the maximum character limit in input field −
Syntax
Following is the syntax to set the maximum character limit in input field.
<input maxlength="enter_number">
Example
Following is the example program to set the maximum character limit in input field −
<!DOCTYPE html> <html> <head> <title></title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action = "#"> Email Id: <br> <input type = "text" name = "email_id" maxlength = "30" /> <br> Password: <br> <input type = "password" name = "password" maxlength = "10" /> <br> <input type = "submit" value ="Submit" /> </form> </body> </html>
Setting the MinimumCharacter Limit
Now, we see how to set the minimum character limit in input field;
Syntax
Following is the syntax to set minimum character limit in input field in JavaScript −
<input minlength="enter_number">
Example
Following is the example program to set minimum character limit in input field in JavaScript −
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="#"> Username: <input type="text" name="username" minlength="10"><br><br> Password: <input type="password" name="password" minlength="8"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
Using Both Maximum andMinimum Characters in an Input Field
Now let us see how to use both minimum limit character and maximum limit character in a input field −
Example
Following is the example program to set minimum and maximum character limit in input field in JavaScript −
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="#"> Username: <input type="text" name="username" minlength="10" maxlength="20"><br><br> Password: <input type="password" name="password" minlength="8" maxlength="10"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
- Related Articles
- How to limit the number of characters entered in a textarea in an HTML form?
- How to give a limit to the input field in HTML?
- How to limit input text length using CSS3?
- How to limit the amount of characters returned from a field in a MongoDB?
- How to limit the amount of characters returned from a field in a MongoDB query?
- Which characters are NOT allowed in MongoDB field names?
- How can we limit the number of characters inside a JTextField in Java?
- How do we add the maximum number of characters allowed in an element in HTML?
- Limit number of values in a field with MongoDB?
- Limit number of values in a field using MongoDB?
- Retrieve first 40 characters of a text field in MySQL?
- HTML DOM Input Text form Property
- How to limit text length of EditText in Android?
- How to use a textarea (a multi-line text input field) in HTML?
- How to count number of characters in Text box while typing in iOS?
