Check Email Address with JavaScript and Regular Expressions

Posted In Javascript - By admin On Tuesday, July 28th, 2009 With 1 Comment

In forms when using email ID fields it is a good idea to use client side validation along with your programming language validation.The following script shows how you can validate an email address for a form

<script language=”javascript”>
function checkEmail()
{
var email = document.getElementById(‘emailID‘);
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value))
{
alert(‘Invalid email address‘);
return false;
}
}
</script>

How can use this script?

<input type=”text” name=”txt” id=”emailID” />
<input type=”button” name=”submit” onclick=”return checkEmail()” value=”Test”/>

About -

Displaying 1 Comments
Have Your Say

  1. Niraj says:

    Very useful script
    Thanks….

    [Reply]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>