简体   繁体   English

用户名正则表达式验证问题

[英]Issue with username regex validation

I searched SO before posting, but couldn't find an answer that would work for me. 我在发布之前搜索了SO,但找不到适合我的答案。

Below is a simple JS script to test a username. 以下是测试用户名简单JS脚本。

The username length validation work fine BUT the character validation doesn't: 用户名长度验证可以正常工作,但字符验证不能:

All non-allowed characters (such as $%;[+)) pass whereas they should return an error. 所有不允许的字符(例如$%; [+))都可以通过,但它们应该返回错误。

<script type="text/javascript">
 $(document).ready(function() {
    $("#username").change(function() {
        var username = $("#username").val();
        var msgbox = $("#status");
        var usernameRegex = /^[a-zA-Z0-9][a-zA-Z0-9\._-]$i/

         if ((username.length >= 4 && username.length <= 20) &&  
 (usernameRegex.test(username.value) == false)) {

            $.ajax({
                type: "POST",
                url: "username.php",
                data: "username=" + username,
                success: function(msg) {

                    if (msg == 'OK') {

                        msgbox.html('<img src="ok.png">');

                    } else {
                        msgbox.html(msg);
                    }
                }
             });
        } else {

            $("#status").html('<img src="no.png">Try again');
        }
     });
});
</script>

What am I missing? 我想念什么?

You are examining the results of your test backwards. 您正在向后检查测试结果。 The test() method returns true if the pattern matches. 如果模式匹配,则test()方法返回true And you are checking for a false value. 并且您正在检查错误的值。

Change it to: 更改为:

if ((username.length >= 4 && username.length <= 20) 
    &&  usernameRegex.test(username.value)) {

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FRegExp%2Ftest https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test?redirectlocale=zh-CN&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FRegExp%2Ftest

/^[a-zA-Z0-9][a-zA-Z0-9\._-]{4,20}$i/
                                  ^^
                        Here is the problem

You probably wanted to specify the case-insensitive flag but by accident you put i inside the regular expression pattern itself, you need to modify your expression to this: 您可能想指定不区分大小写的标志,但是偶然地将i放在正则表达式模式本身中,您需要将表达式修改为:

/^[a-zA-Z0-9][a-zA-Z0-9\._-]{4,20}$/i

and as they guys have pointed out you should test for a true condition or in other words, you need to check if the string actually matches the pattern and thus contains only the characters you allow. 正如他们指出的那样,您应该测试一个true条件,换句话说,您需要检查字符串是否确实与模式匹配,从而仅包含允许的字符。

There's a problem with your regular expression, and it's not just the $i/ . 您的正则表达式有问题,不仅是$i/ You have: 你有:

var usernameRegex = /^[a-zA-Z0-9][a-zA-Z0-9\._-]$i/;

That second token, [a-zA-Z0-9\\._-] , is only evaluated once. 第二个令牌[a-zA-Z0-9\\._-]仅被评估一次。 It checks the first character for the first token, then the second character for the second token, and then expects the end of the string because of $ . 它检查第一个字符的第一个令牌,然后检查第二个字符的第二个令牌,然后由于$原因, 期望字符串的结尾 Even after changing your test condition to true and fixing the rest of the logic, your original regular expression will fail all usernames above two characters. 即使在将测试条件更改为true并修复了其余逻辑之后,您的原始正则表达式也会使所有用户名超过两个字符而失败。

Try this instead: 尝试以下方法:

var usernameRegex = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+$/i;

The + ensures that the regex will look for at least one character in the token, and match them until the string ends. +确保正则表达式将在令牌中查找至少一个字符,并将其匹配直到字符串结束。

Finally, as others have mentioned, you need usernameRegex.test(username) instead of usernameRegex.test(username.value) , and it needs to test for a true value, not false . 最后,就像其他人提到的那样,您需要usernameRegex.test(username)而不是usernameRegex.test(username.value) ,并且它需要测试一个true值,而不是false

See a working demo here. 在此处查看有效的演示。

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

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