Email validation using jQuery

后端 未结 30 2212
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 16:52

I\'m new to jQuery and was wondering how to use it to validate email addresses.

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 17:39

    For thoose who want to use a better maintainable solution than disruptive lightyear-long RegEx matches, I wrote up a few lines of code. Thoose who want to save bytes, stick to the RegEx variant :)

    This restricts:

    • No @ in string
    • No dot in string
    • More than 2 dots after @
    • Bad chars in the username (before @)
    • More than 2 @ in string
    • Bad chars in domain
    • Bad chars in subdomain
    • Bad chars in TLD
    • TLD - addresses

    Anyways, it's still possible to leak through, so be sure you combine this with a server-side validation + email-link verification.

    Here's the JSFiddle

     //validate email
    
    var emailInput = $("#email").val(),
        emailParts = emailInput.split('@'),
        text = 'Enter a valid e-mail address!';
    
    //at least one @, catches error
    if (emailParts[1] == null || emailParts[1] == "" || emailParts[1] == undefined) { 
    
        yourErrorFunc(text);
    
    } else {
    
        //split domain, subdomain and tld if existent
        var emailDomainParts = emailParts[1].split('.');
    
        //at least one . (dot), catches error
        if (emailDomainParts[1] == null || emailDomainParts[1] == "" || emailDomainParts[1] == undefined) { 
    
            yourErrorFunc(text); 
    
         } else {
    
            //more than 2 . (dots) in emailParts[1]
            if (!emailDomainParts[3] == null || !emailDomainParts[3] == "" || !emailDomainParts[3] == undefined) { 
    
                yourErrorFunc(text); 
    
            } else {
    
                //email user
                if (/[^a-z0-9!#$%&'*+-/=?^_`{|}~]/i.test(emailParts[0])) {
    
                   yourErrorFunc(text);
    
                } else {
    
                    //double @
                    if (!emailParts[2] == null || !emailParts[2] == "" || !emailParts[2] == undefined) { 
    
                            yourErrorFunc(text); 
    
                    } else {
    
                         //domain
                         if (/[^a-z0-9-]/i.test(emailDomainParts[0])) {
    
                             yourErrorFunc(text); 
    
                         } else {
    
                             //check for subdomain
                             if (emailDomainParts[2] == null || emailDomainParts[2] == "" || emailDomainParts[2] == undefined) { 
    
                                 //TLD
                                 if (/[^a-z]/i.test(emailDomainParts[1])) {
    
                                     yourErrorFunc(text);
    
                                  } else {
    
                                     yourPassedFunc(); 
    
                                  }
    
                            } else {
    
                                 //subdomain
                                 if (/[^a-z0-9-]/i.test(emailDomainParts[1])) {
    
                                     yourErrorFunc(text); 
    
                                 } else {
    
                                      //TLD
                                      if (/[^a-z]/i.test(emailDomainParts[2])) {
    
                                          yourErrorFunc(text); 
    
                                      } else {
    
                                          yourPassedFunc();
    }}}}}}}}}
    

提交回复
热议问题