Jquery Value match Regex

This question already has an answer here:

  • How to validate an email address in JavaScript? 68 answers

  • Pass a string to RegExp or create a regex using the // syntax
  • Call regex.test(string) , not string.test(regex)
  • So

    jQuery(function () {
        $(".mail").keyup(function () {
            var VAL = this.value;
    
            var email = new RegExp('^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$');
    
            if (email.test(VAL)) {
                alert('Great, you entered an E-Mail-address');
            }
        });
    });
    

    Change it to this:

    var email = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i;
    

    This is a regular expression literal that is passed the i flag which means to be case insensitive.

    Keep in mind that email address validation is hard (there is a 4 or 5 page regular expression at the end of Mastering Regular Expressions demonstrating this) and your expression certainly will not capture all valid e-mail addresses.

    链接地址: http://www.djcxy.com/p/16496.html

    上一篇: 用JavaScript验证电子邮件

    下一篇: Jquery值匹配正则表达式