Regular expressions can be used with JavaScript to validate a form email address entry.
The function below accepts a form and performs the text on the textbox field with the ID of Email.
function validateEmail(frmAddress) {
//Validating the email field
var rgxp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (! frmAddress.Email.value.match(rgxp)) {
alert("Invalid email address");
frmAddress.Email.focus();
frmAddress.Email.select();
return (false);
}
return(true);
}
The function checks the passed variable frmAddress against the regular expression rgxp. If it doesn’t match an alert is triggered and focus is given to the email field for the user to correct their entry.


