C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » General »

Comprehensive and Quicker Validation of Email Address


Posted Date: 12 Apr 2007    Resource Type: Articles    Category: General
Author: Vasudevan Deepak KumarMember Level: Diamond    
Rating: 1 out of 5Points: 10



Introduction


Consider any web application now. It has an Email field in its registration or contact form. A search in Google or your favorite search engine would bring you a lot of Email Validation functions. JavaScript email validation functions mainly places a thrust on syntax level email validations. I have also seen some validation functions which only validates for .com, .net, .org domains. However be advised that there are also a lot of country-specific domains like .IN, .TK, .PK, .AU etc.

This article makes a humble endeavor to present a simplistic regular expression approach to encompass the need and requirement of an elegant syntax level email validation function.


Pre-Requisites and Desired Features


1. Comprehensive Email Validation Encompassing all types of domains (country levels like IN, TK etc) besides the more common COM, NET etc
2. Use quicker Regular Expressions.
3. Avoiding costly loops which take more CPU horsepower on slower PCs.
4. Avoiding too much of string manipulation functions which are error prone.


The solution



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Page title</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
function IsSyntacticallyValidEmail(str) {
// are regular expressions supported?
var supported = 0;
if (window.RegExp) {
var tempStr = "a";
var tempReg = new RegExp(tempStr);
if (tempReg.test(tempStr)) supported = 1;
}
if (!supported)
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
return (!r1.test(str) && r2.test(str));
}
var strAdd = "";
strAdd = prompt("Enter an email address to validate","");

while (strAdd != null)
{
if (IsSyntacticallyValidEmail(strAdd))
{
alert (strAdd +" has passed syntax validations");
}
else
{
alert (strAdd +" might not be a valid email address");
}
strAdd = prompt("Enter an email address to validate","");
}
//-->
</script>



</body>
</html>



This code makes use of a comprehensive Regular Expression that addresses the prerequisites and requirements outlined in the previous section.

Limitations


The following are the limitations of JavaScript Email Validation.

1. It does only a syntax level validation. The actual verification at the mailserver is not done. I would suggest that for actual validations, there are two options. You can use hosted webservices like the ones at http://www.hexillion.com/ can be used. Or Email verification methods that send an email with a hash code asking the user to confirm reciept of the email can be deployed. If the user can not respond within a stipulated time, then the email can be considered invalid.
2. It uses Regular Expression library, which JavaScript 1.2 or higher alone can understand and execute.

Summary


Hence we have discussed a comprehensive syntax level email validation routine in Javascript.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: The Essence of essential string replace in Javascript
Previous Resource: Java script For Mask Date Control in Web application
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use