1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Multiple emails validation in one text box with comma

Discussion in 'JavaScript' started by kumiko, Feb 6, 2008.

  1. #1
    hi there..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(,) or this (. then how can i use the javascxript to do a validation for it? plz help..i duno how to code it. plz help on code.thanx alot
    SEMrush
     
    kumiko, Feb 6, 2008 IP
    SEMrush
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hi, you can use the javascript split() function on the value of the text box your using. I built a simple example for you to follow, this validates all the emails that are seperated by a comma-

    
    <html>
    <body>
    <script language="javascript">
    function checkEmail(email) {
    var regExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
    return regExp.test(email);
    }
    
    function checkEmails(){
    	var emails = document.getElementById("emails").value;
    	var emailArray = emails.split(",");
    	for(i = 0; i <= (emailArray.length - 1); i++){
    		if(checkEmail(emailArray[i])){
    			//Do what ever with the email.
    		}else{
    			alert("invalid email: " + emailArray[i]);
    		}
    	}
    }
    </script>
    <input name="emails" id="emails" type="text" size="50" /><br />
    <a href=javascript:checkEmails();>GO</a>
    </body>
    </html>
    
    Code (markup):
     
    ToddMicheau, Feb 6, 2008 IP
  3. kumiko

    kumiko Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi toddmicheau, thanx for help..by the way.
    let say i have insert TWO Emails like :alice@yahoo, samm@hotmail
    so from these two emails, there are wrong format. from the code u have teach it will alert TWO times coz got TWO error Email. how can i put all the error in one alert? can u plz teach me more? thanx
     
    kumiko, Feb 7, 2008 IP
  4. locdev

    locdev Active Member

    Messages:
    171
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    try this code ;)

    <html>
    <body>
    <script language="javascript">
    function checkEmail(email) {
    var regExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
    return regExp.test(email);
    }
    
    function checkEmails(){
    	var emails = document.getElementById("emails").value;
    	var emailArray = emails.split(",");
    var hasErrors=false;
    var errorMessage="";
    	for(i = 0; i <= (emailArray.length - 1); i++){
    		if(checkEmail(emailArray[i])){
    			//Do what ever with the email.
    		}else{
    			hasErrors=true;
    			errorMessage+="invalid email: " + emailArray[i]+"\n\r";
    		}
    	}
    	if(hasErrors){
    		alert(errorMessage);
    	}
    }
    </script>
    <input name="emails" id="emails" type="text" size="50" /><br />
    <a href=javascript:checkEmails();>GO</a>
    </body>
    </html>
    Code (markup):
     
    locdev, Feb 7, 2008 IP
  5. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Sure, that can be done by storing the invalid emails inside of a variable, then at the end of the functions execution check to see if that variable has data (meaning there were some invalid emails), if it has data in it then just alert the variable. I've modified the function for you below:

    
    <html>
    <body>
    <script language="javascript">
    function checkEmail(email) {
    var regExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
    return regExp.test(email);
    }
    
    function checkEmails(){
    	var emails = document.getElementById("emails").value;
    	var emailArray = emails.split(",");
    	var invEmails = "";
    	for(i = 0; i <= (emailArray.length - 1); i++){
    		if(checkEmail(emailArray[i])){
    			//Do what ever with the email.
    		}else{
    			invEmails += emailArray[i] + "\n";
    		}
    	}
    	if(invEmails != ""){
    		alert("Invalid emails:\n" + invEmails);
    	}
    }
    </script>
    
    <input name="emails" id="emails" type="text" size="50" /><br />
    <a href=javascript:checkEmails();>GO</a>
    </body>
    </html>
    
    Code (markup):
     
    ToddMicheau, Feb 7, 2008 IP
  6. patil07

    patil07 Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the above code sample.

    This code states that the emails are split by a 'comma'. What if the user enters some other character, say a slash (/ or \ ), or anything else?

    How would you validate that?

    Best regards,

    patil07 :)
     
    patil07, Mar 20, 2008 IP