CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Location
    India
    Posts
    24

    Lightbulb Javascript email validation

    Hi,
    Any body who nows an efficient email validation algorithm in javascript.

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Javascript email validation

    HTML Code:
    <script language = "Javascript">
    function 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")
       return false
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert("Invalid E-mail ID")
       return false
    }
    
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail ID")
        return false
    }
    
    if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail ID")
        return false
    }
    
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail ID")
        return false
    }
    
    if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail ID")
        return false
    }
    		
    if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail ID")
        return false
    }
    
    return true					
    }
    
    function ValidateForm(){
    var emailID=document.frmSample.txtEmail
    	
    if ((emailID.value==null)||(emailID.value=="")){
      alert("Please Enter your Email ID")
      emailID.focus()
      return false
    }
    if (echeck(emailID.value)==false){
      emailID.value=""
      emailID.focus()
      return false
    }
    return true
    }
    </script>

  3. #3
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: Javascript email validation

    There is a regular expression designed for this ... I would have to search for it, but with olivthill's code, there exist some email addresses that are valid that will be told are invalid.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured