Regular Expressions
Regular Expressions give you more power to validate strings in a script.
With regular expressions you can create a pattern of what a valid
value can be and test what was entered against it.
For instance, you want to find out if a required field has data in it. Previous examples have
shown how to check for an empty string or a space. However if two spaces were entered, the field would
be valid. Look at the following pattern /^[ ]*$/.
- all patterns need to be surrounded by forward slashes "/"
- ^ match at the beginning of the field
- [ ] create a character set with a space in it
- * match the character preceding if it occurs zero or more times
- $ match at the end of the field
- in other words: check from the beginning of the field to the end of the field for all spaces
function isValidField(FirstName){
var chkName = /^[ ]*$/;
if (chkName.test(FirstName)){
alert("First Name cannot be blank");
return false;
}
else
return true;
}
Click here to see the demo.
You want to find out if a valid email address has been entered. A normal email address
has a combination of at least 3 characters and/or numbers before and after the "@" sign, followed
by a ".". JavaScript string
methods could be used to parse the information before and after the "@" and before the ".". This could
be quite cumbersome.
/^[A-Za-z0-9]{2,}@[A-Za-z0-9_]{3,}\.[A-Za-z]{2}/
Look at the pattern to validate an email address.
- ^[A-Za-z0-9] this pattern says the first character can be any upper case letter A-Z or lower case letter a-z or
a number 0-9.
- {2,} match the preceding patter for at least 2 or more characters
- @must contain a @ character
- [A-Za-z0-9_] the next character can be any upper case letter A-Z or lower case letter a-z or
a number 0-9 or an underscore.
- {3,} match the preceding patter for at least 3 or more characters
- \.must contain a . character, since the dot is a special character it must be preceded by a backslash "\"
- [A-Za-z]the next character can be any upper case letter A-Z or lower case letter a-z
- {2}match the preceding patter for at least 2 characters
Click here to see the demo.
Validate a phone number (999) 999-9999.
/(^[1-9][0-9]{2})([1-9][0-9]{2})([0-9]{4}$)/
- ^[1-9] the first character must be a number between 1-9.
- [0-9]{2} next two numbers must be between 0-9
- [1-9] the next number is between 1-9
- [0-9]{2} next two numbers must be between 0-9 .
- [0-9]{4}$ the last four numbers are between 0-9
- () notice the parentheses dividing the numbers into sections. The data is stored
in memory so it can be used later. This can be used to put the data back together again
Click here to see the demo.
Remove Carriage Return and Line feed characters from a textarea.
/\r\n/g
- /\r\n/ \r is a carriage return and \n is a line feed, we are looking for these two characters together.
- /\r\n/g adding a g after the expression means look for it globally.
Click here to see the demo.
Validate a phrase for alphanumeric characters (from the book).
/[^a-z\d ]/i
- /[^a-z]/i match any character that is not between a-z, Note because there is a i to the end of the
expression, the pattern is case insensitive. So match any character that is not between a-z and A-Z
- /[^a-z\d]/i match any character that is not between a-z, A-Z, or 0-9.
- /[^a-z\d ]/i match any character that is not between a-z, A-Z, 0-9, or a space.
Click here to see the demo.
Modify the demo to remove the not '!'
Click here to see the demo.