Tuesday 26 January 2016

Simple Custom Email Validation (JavaScript)

Simple Custom Email Validation (JavaScript)

Description:    The following validateEmail() JavaScript function checks whether an email address is valid or not. It takes the email address from an input tag with ID="Email" and performs the following checks on it.

            i) Checks the presence of '@'character
           ii) Checks the presence of at least one letter (A-Z or a-z) right                    after the @'character
          iii) Checks the presence of ".com" 


JavaScript Code

function validateEmail() {
    
     document.getElementById('Email').value;
     length = email.length;

     for (i = 0; i < length; i++) {
           if (email[i] == '@')
                break;
     }

     if (i != length) {
           i++;
           if ((email[i] < 'A' || email[i] > 'Z') && (email[i] < 'a' || email[i] > 'z'))
                return false;
           else {
                com = ".com";
                     for (i; i < length; i++) {
                           for (j = 0, k = i; j < 4; j++, k++)
                                if (email[k] != com[j])
                                     break;
                           if (j == 4)
                                break;
                     }
                if (i != length)
                     return true;
                else
                     return false;
           }

     }
     else
           return false;
}

No comments

Post a Comment

Recent Posts