1. I've started a massive SPAM purge, any accounts that have spammed are being removed, also for a short period new user signups are disallowed until the SPAM cleaning is finished.

Validate E-Mail

Discussion in 'Java Script/AJAX' started by JavaScript, Dec 7, 2009.

Thread Status:
Not open for further replies.
  1. JavaScript

    JavaScript New Member

    Joined:
    Nov 25, 2009
    Messages:
    16
    Likes Received:
    1
    Trophy Points:
    0
    This JavaScript verifies that a string looks like a real e-mail address.... detail at JavaScriptBank. com - 2.000+ free JavaScript codes

    [​IMG]
    Demo: JavaScript Validate E-Mail

    How to setup

    Step 1: Copy & Paste JavaScript code below in your HEAD section
    JavaScript
    Code (Text):
    <script language="javascript">
    // Created by: Francis Cocharrua :: hxxp ://scripts.franciscocharrua. com/

    function Validate_String(string, return_invalid_chars) {
      valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      invalid_chars = '';
      if(string == null || string == '')
         return(true);

      //For every character on the string.  
      for(index = 0; index < string.length; index++) {
        char = string.substr(index, 1);                        
         
        //Is it a valid character?
        if(valid_chars.indexOf(char) == -1) {
          //If not, is it already on the list of invalid characters?
          if(invalid_chars.indexOf(char) == -1) {
            //If it's not, add it.
            if(invalid_chars == '')
              invalid_chars += char;
            else
              invalid_chars += ', ' + char;
          }
        }
      }
               
      //If the string does not contain invalid characters, the function will return true.
      //If it does, it will either return false or a list of the invalid characters used
      //in the string, depending on the value of the second parameter.
      if(return_invalid_chars == true && invalid_chars != '') {
        last_comma = invalid_chars.lastIndexOf(',');
        if(last_comma != -1)
          invalid_chars = invalid_chars.substr(0, $last_comma) +
          ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
        return(invalid_chars);
        }
      else
        return(invalid_chars == '');
    }


    function Validate_Email_Address(email_address){
      // Modified and tested by Thai Cao Phong, JavaScriptBank. com
      //Assumes that valid email addresses consist of user_name@domain.tld
     
      at = email_address.indexOf('@');
      dot = email_address.indexOf('.');

      if(at == -1 ||
        dot == -1 ||
        dot <= at + 1 ||
        dot == 0 ||
        dot == email_address.length - 1)
      {
        alert("Invalid email");
        return(false);
      }
         
      user_name = email_address.substr(0, at);
      domain_name = email_address.substr(at + 1, email_address.length);                  

      if(Validate_String(user_name) === false || Validate_String(domain_name) === false)
      {
        alert("Invalid email");
        return(false);
      }

      alert("Valid email");//return(true);
    }
    </script>
        <!--
            This script downloaded from w w w.JavaScriptBank. com
            Come to view and download over 2000+ free javascript at w w w.JavaScriptBank. com
        -->
     
    Step 2: Copy & Paste HTML code below in your BODY section
    HTML
    Code (Text):
    <form name=f>
    <input type=text name=mail value="">
    <input type=button value=Check onclick="Validate_Email_Address(document.f.mail.value)">
    </form>
        <!--
            This script downloaded from w w w.JavaScriptBank. com
            Come to view and download over 2000+ free javascript at w w w.JavaScriptBank. com
        -->
     





    JavaScript Countdown Timer - JavaScript Currency Format - JavaScript Format Phone Number​
    Loading...
  2. yvangkwhen

    yvangkwhen New Member

    Joined:
    Dec 7, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    thnx so much for ur post. it helps me a lot.
  3. hbstone

    hbstone New Member

    Joined:
    Dec 24, 2009
    Messages:
    1
    Likes Received:
    2
    Trophy Points:
    0
    Regular expressions can make that a lot simpler. This should validate/invalidate the exact same e-mail addresses as above, without having to manually verify every character. The behavior of returning invalid characters is there as well.

    Code (Text):

    function validateString(input, returnInvalid) {
        var valid = /[0-9a-z~_\-\.\^]+@[0-9a-z~_\-\^]+\.[0-9a-z\~\_\-\^]+/i,
            invalid = /[^0-9a-z@~_\-\.\^]/i,
            badChars = invalid.exec(input) || [];
        return returnInvalid ? badChars.join(', ') : (!input.length || valid.test(input));
    }

    function validateEmailAddress(address) {
        return validateString(address || '');
    }
     
    2 people like this.
  4. SeoKungFu

    SeoKungFu Active Member

    Joined:
    Jul 9, 2009
    Messages:
    693
    Likes Received:
    98
    Trophy Points:
    28
    +1 for the regex ;) !
Thread Status:
Not open for further replies.

Share This Page