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
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):
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
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):
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):
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