Java Reference
In-Depth Information
You can specify just one of the flags if you want—such as the following:
var myRegExp = new RegExp("hello\\b","i");
or
var myRegExp = new RegExp("hello\\b","g");
Form Validation Module
trY it out
In this Try It Out, you create a set of useful JavaScript functions that use regular expressions to validate
the following:
Telephone numbers
Postal codes
E‐mail addresses
The validation only checks the format. So, for example, it can't check that the telephone number
actually exists, only that it would be valid if it did.
First is the .js code file with the input validation code. Please note that the lines of code in the
following block are too wide for the topic—make sure each regular expression is contained on one
line:
function isValidTelephoneNumber(telephoneNumber) {
var telRegExp = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5})
?\d{3}?\d{0,7}( (x|xtn|ext|extn|pax|pbx|extension)?\.? ?\d{2-5})?$/i;
return telRegExp.test(telephoneNumber);
}
 
function isValidPostalCode(postalCode) {
var pcodeRegExp = /^(\d{5}(-\d{4})?|([a-z][a-z]?\d\d?|[a-z{2}\d[a-z])
?\d[a-z][a-z])$/i;
return pcodeRegExp.test(postalCode);
}
 
function isValidEmail(emailAddress) {
var emailRegExp = /^(([^<>()\[\]\\.,;:@"\x00-\x20\x7F]|\\.)+|("""([^\x0A\x0D"\\]
|\\\\)+"""))@(([a-z]|#\d+?)([a-z0-9-]|#\d+?)*([a-z0-9]
|#\d+?)\.)+([a-z]{2,4})$/i;
return emailRegExp.test(emailAddress);
}
Save this as ch6 _ example6.js .
To test the code, you need a simple page:
<!DOCTYPE html>
 
<html lang="en">
Search WWH ::




Custom Search