简体   繁体   English

jQuery Regex不触发if语句

[英]jQuery Regex not triggering if statement

** EDIT ** The regex was not accepting lowercase. **编辑**正则表达式不接受小写字母。 Adding i did the trick. 添加我的把戏。 Additionally I had to escape the \\ out again in order for the past part of the regex to work. 另外,为了使正则表达式的最后一部分正常工作,我不得不再次将\\退出。

Working one for jquery is following: 工作的jQuery如下:

var email = new RegExp('^[A-Z0-9._%+-]+@[A-z0-9.-]+\\.[A-Z]{2,4}$', 'i');

I want to verify an email with a regex. 我想用正则表达式验证电子邮件。

In my current code I see that it is executing the function but not going into the if statement even though the regex matches. 在我当前的代码中,我看到它正在执行函数,但是即使正则表达式匹配也不会进入if语句。

$('#email').keyup(function() {
    var INPUT = $(this).val();
    var email = new RegExp('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$');
    if (email.test(INPUT)) {
        console.log('inside the if');
    }
    console.log('Outside the if');
});

Regex seems fine, just need to set the case insensitive flag: 正则表达式看起来不错,只需要设置不区分大小写的标志即可:

$('#email').keyup(function() {
    var INPUT = $(this).val();
    var email = new RegExp('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$', 'i');
    if (email.test(INPUT)) {
        console.log('inside the if');
    }
    console.log('Outside the if');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM