martin_narg
07-22-2002, 12:47 PM
This is a simple, but very effective email validator. It checks for invalid characters, and that an @ symbol is present and in the right place, as well as checking for a . to be in the right place.
the function returns true/false.
a simple way to call the function would be:
<form name="myForm" method="POST" action="myFormHandler.html" onSubmit="return checkEmail(document.myForm.myEmail.value)">
<input type="text" name="myEmail">
</form>
function checkEmail(email) {
var str = new String(email);
var isOK = true;
rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
if( rExp.test(str) )
isOK = false;
if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
isOK = false;
if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
isOK = false;
if( str.slice(0,str.indexOf('@')).length < 1 )
isOK = false;
if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
isOK = false;
if( !isOK )
alert( "Invalid email address" );
return isOK;
}
the function returns true/false.
a simple way to call the function would be:
<form name="myForm" method="POST" action="myFormHandler.html" onSubmit="return checkEmail(document.myForm.myEmail.value)">
<input type="text" name="myEmail">
</form>
function checkEmail(email) {
var str = new String(email);
var isOK = true;
rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
if( rExp.test(str) )
isOK = false;
if( str.indexOf('.') == -1 || str.indexOf('@') == -1 )
isOK = false;
if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
isOK = false;
if( str.slice(0,str.indexOf('@')).length < 1 )
isOK = false;
if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 )
isOK = false;
if( !isOK )
alert( "Invalid email address" );
return isOK;
}