Validating email addresses using jQuery and regex

后端 未结 10 2452
情歌与酒
情歌与酒 2020-11-22 17:13

I\'m not too sure how to do this. I need to validate email addresses using regex with something like this:

[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$         


        
相关标签:
10条回答
  • 2020-11-22 17:29

    Try this

    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
        return pattern.test(emailAddress);
    };
    
    0 讨论(0)
  • 2020-11-22 17:37
    $(document).ready(function() {
    
    $('#emailid').focusout(function(){
    
                    $('#emailid').filter(function(){
                       var emil=$('#emailid').val();
                  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if( !emailReg.test( emil ) ) {
                    alert('Please enter valid email');
                    } else {
                    alert('Thank you for your valid email');
                    }
                    })
                });
    });
    
    0 讨论(0)
  • 2020-11-22 17:47

    I would recommend that you use the jQuery plugin for Verimail.js.

    Why?

    • IANA TLD validation
    • Syntax validation (according to RFC 822)
    • Spelling suggestion for the most common TLDs and email domains
    • Deny temporary email account domains such as mailinator.com

    How?

    Include verimail.jquery.js on your site and use the function:

    $("input#email-address").verimail({
        messageElement: "p#status-message"
    });
    

    If you have a form and want to validate the email on submit, you can use the getVerimailStatus-function:

    if($("input#email-address").getVerimailStatus() < 0){
        // Invalid email
    }else{
        // Valid email
    }
    
    0 讨论(0)
  • 2020-11-22 17:47

    We can also use regular expression (/^([\w.-]+)@([\w-]+)((.(\w){2,3})+)$/i) to validate email address format is correct or not.

    var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
     var valid = emailRegex.test(emailAddress);
      if (!valid) {
        alert("Invalid e-mail address");
        return false;
      } else
        return true;
    
    0 讨论(0)
提交回复
热议问题