Checking for numbers and letters
Sometimes situations arise ( suppose a user id, password or a code) when the user should fill a single or more than one field with alphabet characters (A-Z or a-z) and numbers (0-9) in an HTML form. You can write a JavaScript form validation script to check whether the required field(s) in the HTML form contains only letters and numbers.
Javascript function to check if a field input contains letters and numbers only
To get a string contains only letters and numbers (i.e. a-z, A-Z or 0-9) we use a regular expression /^[0-9a-zA-Z]+$/ which allows only letters and numbers. Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.+
Flowchart:
HTML Code
Javascript code
CSS Code
Practice the example online
No comments:
Post a Comment