How to validate Multiple email validation script

Discussion in 'JavaScript' started by subha rr, Feb 13, 2009.

  1. #1
    hi.....

    I need some help on this email validation.
    I got a text box, can let the users to input many email with the seprator comma(,).
    Then how can i use the javascript to do a validation for it? plz help..i dont know how to code it. plz help on code.

    This is the script i am using for validation of single email id

    how to write javascript for validating multiple email id's ....

    Pls help me

    regards
    subha
     
    subha rr, Feb 13, 2009 IP
  2. googlehelper

    googlehelper Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try spliting email string into array and then loop to validate them
     
    googlehelper, Feb 14, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    i did reply to your other two threads, this is getting tiresome. if you just can't take the advice and the explicit examples given to you, you should stop asking. within this: http://forums.digitalpoint.com/showthread.php?t=1191775 we even had the example i am including below - in the context of testing a function against a variety of odd email addresses... this is the last time i am helping you - not done for the sake of dong so but as an exercise in vanilla javascript (learning about prototyping and limitations).

    a working example of this is available here: http://fragged.org/dev/checkingMultipleEmails.php
    <html>
    <head>
    <title>Test of unlimited multiple email boxes by D.Christoff of fragged.org</title>
    <script type="text/javascript">
    if (!window.Element ) {
        // fix for IE, but it will still whine if Element is not extened by all pass backs.
    	Element = function() {};
    }
    
    String.prototype.isEmail = function() {
        /* boolean, returns true or false if the string passed through is an email
           example use: var myEmail = 'christoff@securemail.com';
           if (myEmail.isEmail()) { do someting } else { do something else }
        */
        var validEmailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    
        return this.match(validEmailRegex); // regex defined above but it can be local."
    };
    
    String.prototype.contains = function(what) {
        // returns true or false if the string passed through contains text or regex
        return this.match(what);
    };
    
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, '');
    }
    
    // Element.prototype is not implemented in IE versions prior to 8, you need your own functions
    // however - this is only to make the CSS changes so totally irrelevant to the test,
    // feel free to test out.
    Element.prototype.hasClass = function(what) {
        // returns true if the element passed through has the what string in the className
        return this.className.contains(what);
    };
    
    Element.prototype.addClass = function(what) {
        if (!this.hasClass(what))
            this.className += " " + what;
    
        return this;
    };
    
    Element.prototype.removeClass = function(what) {
        if (this.hasClass(what))
            this.className = this.className.replace(what, "").trim();
    };
    
    
    
    //define the empty arrays and the page namespace
    var dodgyEmails = [], emailFields = [], emailTest = {
        init: function() {
            emailFields = document.getElementsByTagName("input");
    
            // intercept the form submission, we check on submit.
            document.getElementById("myform").onsubmit = function() {
                // run the checks.
                emailTest.checkAllEmails();
    
                // need firefox and firebug to output below or do something with the list
                // of invalid emails.
                C.log(dodgyEmails);
    
                return (dodgyEmails.length) ? false : true; // only submit if dodgyEmails are 0.
            };
        },
        checkAllEmails: function() {
            // reset lists of data and dodgy ones
            dodgyEmails = [];
            var emailsCount = emailFields.length;
    
            // get email values from the fields
            while (emailsCount--) {
                if (emailFields[emailsCount].hasClass("emailVerify")) {
                    // reset previous errors....
                    emailFields[emailsCount].removeClass("myError");
    
                    if (!emailFields[emailsCount].value.isEmail()) {
                        // push this into the array of dodgy emails
                        dodgyEmails.push(emailFields[emailsCount].value);
    
                        // add the myError class to the field to indicate a problem
                        emailFields[emailsCount].addClass("myError");
                    }
                }
            };
    
            // now dodgyEmails will contain the data submitted on all emails that are invalid.
        }
    };
    
    var C = {
        // console wrapper by Dimitar Christoff
        debug: true, // global debug on|off
        quietDismiss: false, // may want to just drop, or alert instead
        log: function() {
            if (!C.debug) return false;
    
    
            if (typeof console == 'object' && typeof console.log != "undefined")
                console.log.apply(this, arguments);
            else
                if (!C.quietDismiss) {
                    var result = "";
                    for (var i = 0, l = arguments.length; i < l; i++)
                        result += arguments[i] + " ("+typeof arguments[i]+") ";
    
                    alert(result);
                }
        }
    }; // end console wrapper.
    
    
    window.onload = emailTest.init;
    </script>
    <style>
    .myInput {
        border: 1px solid #000;
        border-bottom: 2px solid #666;
        padding: 2px;
        font-family: verdana, arial;
        font-size: 12px;
        margin-bottom: 2px;
        background: white;
    }
    
    .myError {
        border-bottom: 2px solid #500;
        background: #ccc;
    }
    </style>
    </head>
    <body>
        <h1> this test is done for firefox or opera or safari - browsers that support Element prototyping.</h1>
        You can remove the bits to do with className or replace them with functions like addClass = function(element, args) <br /><br />
    <form id="myform" action="http://fragged.org/">Send AT LEAST 3 valid emails to <a href="http://fragged.org">fragged.org</a> for spam<br /><br />
        Email 1: <input type=text id="email1" name="email1" class="myInput emailVerify" /><br />
        Email 2: <input type=text id="email2" name="email2" class="myInput emailVerify" /><br />
        Email 3: <input type=text id="email3" name="email3" class="myInput emailVerify" /><br />
        Name: <input type=text name="Name" class="myInput" /><br /><br />
        <input type=submit name=done value="Submit" />
    </form>
    </body>
    </html>
    
    HTML:
    p.s. now you see why the email validation function that you picked is inadequate - it is tied to a particular form name and a particular email input field. you can add 50 email fields above and for as long as they have emailVerify as a classname, it will verify them. To do the verification in IE w/o the Element.prototype, just do function hasClass(element, what) { return element.className.test(what); }

    good luck really.
     
    dimitar christoff, Feb 15, 2009 IP