»
EnglishFrenchVietnamese

Print - E-mail Validation - JavaScriptBank.com

Full version: jsB@nk » Email » E-mail Validation
URL: http://www.javascriptbank.com/e-mail-validation.html

E-mail Validation © JavaScriptBank.comThis JavaScript uses regular expressions to check that a string contains a valid email address. Note that it won't catch all invalid emails, like most similar scripts. However, for most intents and purposes, this JavaScript should serve its purpose well, by rejecting common email typos while leaving room for obscure yet valid emails to pass.

Full version: jsB@nk » Email » E-mail Validation
URL: http://www.javascriptbank.com/e-mail-validation.html



JavaScript
<script language=javascript>function validateTOMailID()//Validate TO, CC and BCC Mail-IDs{var flag = true;if(document.form.toEMailID.value != "")//Validate TO Mail-ID{flag = validEMailIDs(document.form.toEMailID.value);}return flag;}function validEMailIDs(mailIDs)//Validate E-Mail-IDs{var valid = true;if(mailIDs.indexOf(";") != -1)//Check wheter Mail-IDs are separated by semicolons{alert("Mail-IDs should be separated by commas.");valid = false;}else{var arrMailID = mailIDs.split(",");//split all Mail-IDs based on comma - Array of Mail-IDsfor(var j=0; j<arrMailID.length; j++)//check for each Mail-ID{// 1+@3+ [or x@x.x] is as close as we will testvar mailID = arrMailID[j];        var mailID = trim(mailID);//Trim Each Mail-IDif(mailID == ""){valid = false;alert("Please remove the un-necessary comma operator(s).");break;}var dot = mailID.lastIndexOf(".");//index of dot symbolvar at = mailID.indexOf("@");//index of at symbolvar dom = dot - at;var space = mailID.indexOf(" ");//index of space charactervar lenMailID=mailID.length;if( (lenMailID < 5) || (space != -1) || (at == -1) || (dot == -1) || (at < 1) || ( dom < 2) ||   (dot == lenMailID-1) || (at == lenMailID-1) ){valid = false;alert("The format of Mail-ID \"" + mailID + "\" is not correct.");break;}}}return valid;}function trim(str){return str.replace( /^\s+/g,'').replace(/\s+$/g,'');}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<script language=javascript>document.write( 'FirstName.LastName@Site.Section.com is a ' + validEMailIDs('FirstName.LastName@Site.Section.com') + ' email<br />' );document.write( 'FullName@Site.com is a ' + validEMailIDs('FullName@Site.com') + ' email<br />' );</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->