1

We have .js email validation function using regex for a textbox. But however our validation allows wrong emails, when user mentions email ID, starting with comma or period symbol. Please help with the code.

function validEmailAddress(emailAddress){   
    var regex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    var result = emailAddress.replace(/\s/g, "").split(/,|;/); 
    alert(result.length);       
    for(var i = 0;i < result.length;i++) {
        if(!regex.test(result[i])) {
            return false;
        }
    }       
    return true;
}
Esko
  • 4,109
  • 2
  • 22
  • 37
  • It seems your function validates a list of emails, not just a single one!? So if the email addresses are separated by a comma it might ignore empty entries that's why a leading comma returns in false validation. – xander Jan 31 '18 at 10:40
  • @xander,what can I do now? Do you have any suggestions? – Keshav_Nandhan Jan 31 '18 at 12:24

0 Answers0