Home > ASP.Net, Jquery > JQuery to Validate Email Format

JQuery to Validate Email Format


Hi

try this example to use jquery to validate Email format


<html xmlns=<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>>
<head id="Head1" >
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<div>
<input id="txtEmailAddress" type="text" />
<br />
<input id="btnSubmit" type="button" value="Check Email" />
<br />
<div id="MessageDiv" style="display: none; background-color: #E8E8EA; width: 200px;">
</div>
</div>
</body>
 
<script type="text/javascript">
$(function()
{var txtEmailAddress = $('#txtEmailAddress');
var messageDiv = $('#MessageDiv');
$('#btnSubmit').click(function(e)
{
txtEmailAddressValue = txtEmailAddress.val();

if (txtEmailAddressValue == '')
{
messageDiv.html('Email address required');
messageDiv.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
txtEmailAddress.focus();
}
else
{
if (!IsValidEmail(txtEmailAddressValue))
{
messageDiv.html('Enter a valid email address');
messageDiv.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
txtEmailAddress.focus();
}
else
{
messageDiv.html('');
messageDiv.hide();
}
}
});

function IsValidEmail(email)
{
var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
return filter.test(email);
}
});
</script>
</html>

Good Luck.

Advertisements
Categories: ASP.Net, Jquery
  1. Ethan
    January 28, 2012 at 2:05 am

    This is fantastic – thanks so much for posting. It worked for me in about 30 seconds.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: