Validation is the process of validating/verifying the user entries against some criteria.
The main purpose is to ensure that the user entries are matching with the policy of the system.
For example, if a site allow visitors below an age of 50, then the validation to ensure the user entered age is less than 50 should be done.
Related : Validation - Wikipedia
Detailed instructions on getting validation set up or installed.
Validating the Name entered by a User contain the following check
So, the regular expression for this is
^[A-Z][a-z]*(\.?\s?[A-Z][a-z]*)+$
This means
^
-> Should start with
[A-Z]
-> the first letter should be capital case
[a-z]*
-> the leading letters should be small case (Optional and not applicable for Initials. Ex : J. Doe)
(\.?\s?[A-Z][a-z]*)+
-> A dot (.) and/or a space (" "), then a capital case and small cases. The last + indicates that this part can repeat many times and at least one time it should be there.
$
end. No further words will be there
Example matching : J. Doe , John Doe, John Doe Doe, John D Doe.
Example in JavaScript
var name = "John Doe";
var noname = "123Abc";
console.log(/^[A-Z][a-z]*(\.?\s?[A-Z][a-z]*)+$/.test(name)); // true
console.log(/^[A-Z][a-z]*(\.?\s?[A-Z][a-z]*)+$/.test(noname)); // false
Let's take the Range is from 18 to 80.
So, to validate,
We should check that the age is a positive integer.
Then check it should be greater than or equal to 18 and less than or equal to 80.
The test to check whether it is a number or not can be performed by a simple regular expression like
^[0-9]+$
Or even simpler
^\d+$
Then we can check it with range as
if age>=18 && age <=80 return true
else return false
Sometimes, we have to take input from users which should contain only alpha numeric characters.
For example, lets say a Username system which allow only letters and numbers,
Then this can be done with the following Regular Expression
^[a-zA-Z0-9]+$
^
is restrict the start[a-zA-Z0-9]+
is the main part which allow only small a-z
capital A-Z
and numbers with a minimum length of 1 to any extend.$
restrict the endThis can also done by using
^[\w\d]+$
Here
\w
represent the alphabets\d
represent the digitsIf we want to restrict the length to maximum 20 charactes,
^[a-zA-Z0-9]{1,20}$
{1,20}
indicate that the length can be between 1 and 20 including both.