使用正则表达式的 jQuery 验证器和可选字段

发布于 2024-12-08 16:16:46 字数 770 浏览 0 评论 0原文

我有一个可选字段(例如“text1”),它可能是空白或仅是字母数字:

jQuery.validator.addMethod("onlyAlphaNumeric", 
        function(value, element) {  
            var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
            return ((this.optional(element)) || regExp.test(value));
        }
    , "Only aplaha-numeric characters allowed");


$("#student-search-form").validate({
    rules : {
        text1 : {
            optional : true,
            onlyAlphaNumeric: "Only a-n allowed"
        }
    },
    messages: {
        text : {                    
            acceptOnly: " Only alpha-numeric characters allowed"
        }               
    }
});

问题是没有进行验证,因此如果用户在“text1”中输入“!&^%(*”,则表单将变为已提交,没有错误检查

有人可以告诉我我做错了什么吗? 谢谢。

I have an optional field (say "text1") which may either be blank or only alpha-numeric:

jQuery.validator.addMethod("onlyAlphaNumeric", 
        function(value, element) {  
            var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
            return ((this.optional(element)) || regExp.test(value));
        }
    , "Only aplaha-numeric characters allowed");


$("#student-search-form").validate({
    rules : {
        text1 : {
            optional : true,
            onlyAlphaNumeric: "Only a-n allowed"
        }
    },
    messages: {
        text : {                    
            acceptOnly: " Only alpha-numeric characters allowed"
        }               
    }
});

The problem is no validation happens, so if user enters "!&^%(*" in 'text1', the form gets submitted, no error checks.

Can somebody please tell me what am I doing wrong?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2024-12-15 16:16:46

这是错误的...

rules : {
    text1 : {
        optional : true,
        onlyAlphaNumeric: "Only a-n allowed"
    }
},

对于 onlyAlphaNumeric:,您只能输入 truefalse

我不太确定 optional: 但我知道 required: 是有效的...所以将 required: 设置为 false.或者,您可能可以完全忽略它,因为验证器默认将字段设置为“可选”(required:false),除非您另外指定。

rules : {
    text1 : {
        required : false,
        onlyAlphaNumeric: true
    }
},

看到这个类似的答案...

使用 jquery 验证插件,如何在文本框上添加正则表达式验证?

This is wrong...

rules : {
    text1 : {
        optional : true,
        onlyAlphaNumeric: "Only a-n allowed"
    }
},

For onlyAlphaNumeric:, you can only put true or false.

I am not too sure about optional: but I know required: is valid... so set required: to false. Alternatively, you could probably leave it out entirely since the validator defaults to fields as being "optional" (required:false) unless you specify otherwise.

rules : {
    text1 : {
        required : false,
        onlyAlphaNumeric: true
    }
},

See this similar answer...

using the jquery validation plugin, how can I add a regex validation on a textbox?

ζ澈沫 2024-12-15 16:16:46

您可以向验证方法添加可选 -

http://ajax。 microsoft.com/ajax/jquery.validate/1.7/additional-methods.js

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, spaces or underscores only please");

验证 -

rules : {
     text1 : {
         alphanumeric: true
     }
 },

you can add optional to the validation method -

http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, spaces or underscores only please");

Validate -

rules : {
     text1 : {
         alphanumeric: true
     }
 },
才能让你更想念 2024-12-15 16:16:46

我认为你的代码一定是这样的:

// Suppose that your method is well defined
jQuery.validator.addMethod("onlyAlphaNumeric", 
    function(value, element) {  
        var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
        return ((this.optional(element)) || regExp.test(value));
    }
, "Only alpha-numeric characters allowed");

$("#student-search-form").validate({
    rules : {
        text1 : {
        required : false, // optional must be replaced by required
        onlyAlphaNumeric: true // rules are boolean
        }
    },
    messages: {
        text1 : {
            onlyAlphaNumeric: "I can change my message : Only alpha-numeric characters allowed"
        }
    }
});

I think that your code must be something like this :

// Suppose that your method is well defined
jQuery.validator.addMethod("onlyAlphaNumeric", 
    function(value, element) {  
        var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
        return ((this.optional(element)) || regExp.test(value));
    }
, "Only alpha-numeric characters allowed");

$("#student-search-form").validate({
    rules : {
        text1 : {
        required : false, // optional must be replaced by required
        onlyAlphaNumeric: true // rules are boolean
        }
    },
    messages: {
        text1 : {
            onlyAlphaNumeric: "I can change my message : Only alpha-numeric characters allowed"
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文