Javascript Email Address Validation example

I convey tried a bunch of JavaScript electronic mail validation routines available on the cyberspace but this ane SmartWebby worked best for me. I convey used it several times it exactly great. This javascript validation code is cross browser compatible (works for all browsers). The next representative shows how you lot tin validate an electronic mail address for a form.

Here is how the JavaScript Email Address Validation operate for the given code:

Function echeck is used to verify if the given value is a possible valid electronic mail address. This business office merely verifies the following:

  1. It makes certain the electronic mail address has alone ane (@) in addition to that it is non at the cease or offset of the electronic mail address.
  2. It makes certain the electronic mail address has atleast ane (.) afterwards the @.
  3. It likewise makes certain that at that spot are no spaces, extra ‘@’s or a (.) exactly earlier or afterwards the @.

Email Validation Example

Enter an Email Address :

JavaScript Code

<script linguistic communication = "Javascript">  business office echeck(str) {   var at="@"   var dot="."    var lat=str.indexOf(at)    var lstr=str.length    var ldot=str.indexOf(dot)    if (str.indexOf(at)==-1){     alert("Invalid E-mail ID")     furnish faux    }   if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){   alert("Invalid E-mail ID")   furnish faux  }   if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){   alert("Invalid E-mail ID")   furnish faux  }   if (str.indexOf(at,(lat+1))!=-1){   alert("Invalid E-mail ID")   furnish faux  }   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){   alert("Invalid E-mail ID")   furnish faux  }   if (str.indexOf(dot,(lat+2))==-1){   alert("Invalid E-mail ID")   furnish faux  }   if (str.indexOf(" ")!=-1){   alert("Invalid E-mail ID")   furnish faux  }   furnish truthful  }  business office ValidateForm(){  var emailID=document.frmSample.txtEmail   if ((emailID.value==null)||(emailID.value=="")){   alert("Please Enter your Email ID")   emailID.focus()   furnish faux  }  if (echeck(emailID.value)==false){   emailID.value=""    emailID.focus()    furnish faux  }  furnish truthful } </script> 

HTML Code

<form name="frmSample" method="post" action="#" onSubmit="return ValidateForm()"> <p>Enter an Email Address :   <input type="text" name="txtEmail"> </p> <p>  <input type="submit" name="Submit" value="Submit"> </p> </form> 

Courtesy of SmartWebby.com

Hosting Unlimited Indonesia


Sumber http://www.mysamplecode.com/