Today lets see how we can make use of pattern attribute to validate the user input data and use title attribute to hint the user for right inputs.
HTML file
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < !DOCTYPE HTML> <html> <head> <title>pattern and title attribute: HTML5</title> <link href="myStyle.css" rel="Stylesheet"/> <meta charset="utf-8"/> </head> <body> <form> <label for="name">Name: </label> <input id="name" type="text" pattern="^[a-zA-Z]+$" title="Example: Satish"/><br /> <label for="age">Age: </label> <input id="age" type="text" pattern="^[0-9]{2}$" title="Example: 25" /><br /> <input type="submit" value="Done"/> </form> </body> </html> |
Here in the first input field, we’re restricting the input to only lower case and upper case alphabets. To the second input field, we’re restricting it to only 2 digits from 0 to 9.
CSS file
myStyle.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | label{ width: 50px; float:left; } input{ width: 200px; } form { padding: 5px; background-color: ivory; width: 300px; height: auto; border: 2px dotted black; } |
CSS styling to align the input fields and the labels, with background color to the form, which has a 2px black dotted border.
pattern and title Attribute of Form Field: HTML5
[youtube https://www.youtube.com/watch?v=TKLF0Aydcv4]
Note: ^ symbol specifies the beginning and the $ symbol specifies the end of the input value.
You can share any simple/small to complex/big regular expression knowledge in the comment section below. Your time and effort is highly appreciated. Your knowledge and inputs will surely be helpful to many people around the world. Comment section is all yours :-)