jQuery验证正则表达式禁止数字和特殊字符

3

我有一个表格,需要一个字段不含任何特殊字符或数字。目前它可以正常工作:

  • abc: 没有错误。
  • 123: 错误。
  • !@#$: 错误。

问题在于当我添加像#$%abcabc123这样的内容时,它没有产生我期望的错误。

我使用的函数如下:

$.validator.addMethod("checkallowedchars", function (value) {
    var pattern = new RegExp('[A-Za-z]+', 'g');
    return pattern.test(value)
}, "The field contains non-admitted characters");

这里有一个我正在使用的函数/正则表达式的JSFiddle示例:http://jsfiddle.net/nmL8maa5/


使用锚点:^[A-Za-z]+$ - rock321987
return /^[A-Za-z]+$/.test(value) - Wiktor Stribiżew
@Adrian,请查看我下面的答案,如果还有不清楚的地方,请告诉我。 - Wiktor Stribiżew
2个回答

1

正确答案之一是:

return /^[A-Z]+$/i.test(value);

并且添加

checkallowedchars: true,

遵循规则。

查看更新的演示fiddle(下面这个在SO上对我来说无法使用,不知道为什么)。

$(document).ready(function () {
    
    $.validator.addMethod("pwcheckallowedchars", function (value) {
        return /^[a-zA-Z0-9!@#$%^&*()_=\[\]{};':"\\|,.<>\/?+-]+$/.test(value) // has only allowed chars letter
    }, "The password contains non-admitted characters");

    $.validator.addMethod("checkallowedchars", function (value) {
        return /^[A-Z]+$/i.test(value);
    }, "The field contains non-admitted characters");

    $.validator.addMethod("pwcheckspechars", function (value) {
        return /[!@#$%^&*()_=\[\]{};':"\\|,.<>\/?+-]/.test(value)
    }, "The password must contain at least one special character");
    
    $.validator.addMethod("pwcheckconsecchars", function (value) {
        return ! (/(.)\1\1/.test(value)) // does not contain 3 consecutive identical chars
    }, "The password must not contain 3 consecutive identical characters");

    $.validator.addMethod("pwchecklowercase", function (value) {
        return /[a-z]/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter");
    
    $.validator.addMethod("pwcheckrepeatnum", function (value) {
        return /\d{2}/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter");
    
    $.validator.addMethod("pwcheckuppercase", function (value) {
        return /[A-Z]/.test(value) // has an uppercase letter
    }, "The password must contain at least one uppercase letter");
    
    $.validator.addMethod("pwchecknumber", function (value) {
        return /\d/.test(value) // has a digit
    }, "The password must contain at least one number");
    
    
    
    
    
    $('#myform').validate({
        // other options,
        rules: {
            "firstname.fieldOne": {
                required: true,
                checkallowedchars: true,
                pwchecklowercase: true,
                pwcheckuppercase: true,
                pwchecknumber: true,
                pwcheckconsecchars: true,
                pwcheckspechars: true,
                pwcheckallowedchars: true,
                minlength: 8,
                maxlength: 20
            }
        }
    });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/additional-methods.js"></script>
<form id="myform">
    <input type="text" name="firstname.fieldOne" /><br/>
    <br/>
    <input type="submit" />
</form>

你的代码有两个问题:

  • 正则表达式没有锚定,当它没有锚定时,它会匹配较大字符串中的部分子字符串。 由于它只匹配1个或多个字母,并且#$%abc包含3个字母,因此满足条件。
  • 第二个问题是你正在使用/g修饰符与RegExp.text()一起使用。 这会导致意外的行为。

0
请使用这个:
^[A-Za-z]+$

解释:

  1. ^ 是字符串的开头锚点。
  2. $ 是字符串的结尾锚点。

您应该在正则表达式中使用它来处理从开头到结尾的字符串,不包括中间部分。


仅仅分享模式是不够的。与正则表达式/代码相关的问题也很重要。 - Wiktor Stribiżew
是的,他使用了 g 标志。我不知道为什么。 - Aminah Nuraini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接