Search code examples
jqueryemail-validation

Validate format of an email address - no regex, no plugins jquery


I've got a function which almost works - it validates an email address format in the following form:

  • Must have an @ symbol
  • Must have some string after @ symbol.
  • Must have a '.' followed by another string after that.

It doesn't work at the moment because I don't know how to correctly insert a variable that accepts any string that the user inputs - how can this be achieved?


Solution

  • I am not going to profess to being an email validation expert.

    If you really must not use regex, could do something along the lines of:

    function validateEmail(str){
       var index_at= str.indexOf('@')
       if( index_at === -1){
           return false;
       }   
    
        var name= str.substr(0,index_at);
         /* should test name for other invalids*/
    
        var domain=str.substr(index_at+1);
        /* should check for extra "@" and any other checks that would invalidate an address of which there are likely many*/
        if( domain.indexOf('@') !=-1){
            return false;
        }
        /* dot can't be first character of domain*/
       return domain.indexOf('.') >0;
    
    }
    

    DEMO: http://jsfiddle.net/v43Hw/

    Strongly recommend using a regex that has been tested against email standards for greater reliability.