Java Reference
In-Depth Information
That was straightforward. Now for the domain name part.
^([^<>()\[\],;:@“\x00-\x20\x7F]|\\.)+@(([a-z]|#\d+?)([a-z0-9-]
|#\d+?)*([a-z0-9]|#\d+?)\.)+([a-z]{2,4})$
We've had to put it on two lines to fi t this topic's page width, but in your code this must all be on one line.
Finally, let's create the function for the JavaScript module.
function isValidEmail( emailAddress )
{
var emailRegExp = /^([^<>()\[\],;:@“\x00-\x20\x7F]|\\.)+
@(([a-z]|#\d+?)([a-z0-9-]|
#\d+?)*([a-z0-9]|#\d+?)\.)+([a-z]{2,4})$/i
return emailRegExp.test( emailAddress );
}
Please note the regular expression must all be on one line in your code.
With the module completed, let's take a look at the code to test the module.
First, the module is linked to the test page like this:
<script type=”text/javascript” src=”ch9_examp7_module.js”></script>
Then each of the three test buttons has its click events linked to the validation functions in the module
as follows:
<input type=”button” name=”cmdIsValidTelephoneNumber”
id=”cmdIsValidTelephoneNumber“
value=”Is Valid Telephone Number?”
onclick=”alert('Is valid is ' +
isValidTelephoneNumber( document.form1.txtString.value ))“ />
<input type=”button” name=”cmdIsValidPostalCode” id=”cmdIsValidPostalCode”
value=”Is Valid Postal Code?”
onclick=”alert('Is valid is ' +
isValidPostalCode( document.form1.txtString.value ))“ />
<input type=”button” name=”cmdIsEmailValid” id=”cmdIsEmailValid”
value=”Is Valid Email?”
onclick=”alert('Is valid is ' + isValidEmail( document.form1.txtString.value ))“ />
So taking telephone validation test button, an onclick event handler is added.
onclick=”alert('Is valid is ' +
isValidTelephoneNumber( document.form1.txtString.value ))“
This shows an alert box returning the true or false value from the isValidTelephoneNumber() func-
tion in your validation module. In a non-test situation, you'd want a more user-friendly message. The
other two test buttons work in the same way but just call different validation functions.
Search WWH ::




Custom Search