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.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|