- If a text input is all alphanumeric characters (numbers & letters)
- If a text input has the correct number of characters in it (useful when restricting the length of a username and/or password)
- If a selection has been made from an HTML select input (the drop down selector)
- If a text input is empty or not
- If a text input is all numbers
- If a text input is all letters
- If an email address is valid
Let’s start with empty field validation. The below code check the length of input textbox and if empty display a message.Listing 1: Script to Validate Empty Field.
Figure 1: Above figure is output of validation of empty field
Email validation scheme tells that how to check if a user's email address is valid. Email validation checks the following points
- Combination of letters, numbers, periods, hyphens, plus signs, and/or underscores
- The at “@” symbol
- A period and top level domain like net, edu, uk, us, uk, com, net, gov.
Let’s go through an example to understand the usage of email validation. The below example demonstrate the validation with help of regex to validate the email.
Listing 2: Email Validation script
Figure 2: Above two figures are example of valid and invalid emails.
This is validation to check that entered values are all letters or combination of different chars. Following script will compile the idea of validation for all letters.
Listing 3: Validation for all letters
Figure 3: Above output checks the entered data is letters or include any other char also.
Tutorial covered three validation scheme. Two of them are using regular expression for matching and the other one is simple so not writing regex but if you want you can write a regex for this case also. There are various validation schemes for checking the other type of validations, like check for all or no, checking for all letters and numbers and restricting the length of field. Now you can explore these areas and comment here in case of any issues.