jQuery to validate Email Address Validation

Here is an example program of validating the Email address using the jQuery API.

jQuery to validate Email Address


In this  jQuery tutorial we will develop a  program that validate the email address  from server and displays on the user browser. In this example we will be calling a server side PHP script to get the "result". You can easily replace PHP with JSP, or ASP program.

Steps to develop the  program for Email Address Validation

Step 1:

Create php file to that prints the current server data. Here is the code of PHP script (emailValidation.php).


$email = $_GET['email'];

function isValidEmail($email){
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);

if(!isValidEmail($email) && $email!="")

echo $errormsg;

Step 2:

Write HTML page to call the emailValidation.php. Create a new file (emailvalidation.html) and add the following code into it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<title>E-Mail Validation</title>
<script type="text/javascript" src="jquery-1.2.6.js">
<script type="text/javascript"> 
function emailValidate()

email = document.getElementById("email").value;

url : "emailvalidation.php?email="+email,
success : function (data) {

if(data=="error1") {
.innerHTML="Please enter email.";
else if(data=="error2")
.innerHTML="Please enter valid email.";

<form name="emailvalidate" action="" method="post" >

<div><input type="hidden" name="query" value="" />
<tr><td colspan=2>&nbsp;<div id="errormsg">

<th align="left"><label for="email">Email</label>
<input type="text" id="email" name="email" size="20" />
<td id="erroremail" class="error">
<td><input type="button" name="Submit" 
value="Submit" onClick="emailValidate();"/></td>




Program explanation:

The following code includes the jQuery JavaScript library file:

<script type="text/javascript" src="jquery-1.2.6.js"></script>

On Click on the Button  emailValidate() funcation is called:

<input type="button" name="Submit" value="Submit" onClick="emailValidate();"/>


The code


url : emailValidation.php

makes ajax call to emailValidation.php file get the Text parameter  "email".

This file call the ValidEmail($email) this match with regular expression return error messages ;

Following code intercepts the ajax call success and then sets the data into "erroremail" div:

success : function (data) {

if data equals to "error1"  It means You not entered the E-mail Address;

if data equals to "error2"  It means You not entered the valid E-mail Address;


When you run the program in browser it will look like following screen shot:


Check online demo of the application

Learn from experts! Attend jQuery Training classes.

Share on Google+Share on Google+

jQuery to validate Email Address Validation

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: jQuery Example, jQuery to Retrieve Server's Current Time, jQuery Server time  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image