简体   繁体   English

jQuery验证插件自定义规则检查单词

[英]jquery validation plugin custom rule check for a word

I need to create a custom rule for the Jquery Validation Plugin that checks for one specific word. 我需要为Jquery Validation插件创建一个自定义规则,以检查一个特定的单词。

In this case, the user must answer a question correctly by entering the word "cat" into the input field. 在这种情况下,用户必须通过在输入字段中输入单词“ cat”来正确回答问题。 The custom rule would need to define the answer as "cat", I was thinking as a variable, and then check for it. 自定义规则将需要将答案定义为“ cat”,我以为是变量,然后对其进行检查。

My failed attempt: 我失败的尝试:

jQuery.validator.addMethod("answercheck", function(value, element) { 
  var password = string('hot');
  return value(password); 
}, "Type the correct answer");

and the setup: 和设置:

rules: {
    input_answer{
        required: true,
        answercheck: true
    }

messages: {
    input_answer{
        required: "Answer the question",
        answercheck: "Your answer is incorrect"
    }

Any ideas? 有任何想法吗?

As others have stated, this is not a secure method since anyone could see/manipulate the JavaScript and easily bypass it. 正如其他人所述,这不是一种安全的方法,因为任何人都可以看到/操纵JavaScript并轻松绕过它。

However, to simply answer your question, here's how it's done. 但是,简单地回答您的问题,这是如何完成的。 (It's also a good learning exercise about using addMethod )... (这也是有关使用addMethod的很好的学习练习)...

jQuery.validator.addMethod('answercheck', function (value, element) { 
    return this.optional(element) || /^\bcat\b$/.test(value);
}, "Type the correct answer");

And you also wouldn't need to specify a message for answercheck below, because it's already defined above within the method itself. 而且,您也不需要在下面为answercheck指定一条消息,因为它已经在method本身中在上面定义了。 (You were also missing some braces { , colons : , and a comma , in this code.) (在此代码中,您还缺少一些花括号{ ,冒号:和一个逗号。)

rules: {
    input_answer: { // <- missing colon after input_answer
        required: true,
        answercheck: true
     }
}, // <- missing brace & comma
messages: {
    input_answer: { // <- missing colon after input_answer
        required: "Answer the question"
        //, answercheck: "Your answer is incorrect"  // not needed, specified in method
    } 
} // <- missing brace and maybe a comma depending on what follows

Working jsFiddle DEMO : 工作jsFiddle DEMO

http://jsfiddle.net/ht8Lu/ http://jsfiddle.net/ht8Lu/


As per the documentation , you'll need to construct your own custom method. 根据文档 ,您将需要构造自己的自定义方法。

Add a custom validation method. 添加自定义验证方法。 It must consist of a name (must be a legal javascript identifier), a javascript based function and a default string message. 它必须由名称(必须是合法的javascript标识符),基于javascript的函数和默认字符串消息组成。

Arguments: 参数:

name; 名称; String The name of the method, used to identify and referencing it, must be a valid javascript identifier 字符串用于标识和引用它的方法名称,必须是有效的javascript标识符

method; 方法; Callback The actual method implementation, returning true if an element is valid. 回调实际的方法实现,如果元素有效,则返回true。 First argument: Current value. 第一个参数:当前值。 Second argument: Validated element. 第二个参数:Validated元素。 Third argument: Parameters. 第三个参数:参数。

message (Optional); 消息(可选); String, Function The default message to display for this method. 字符串,函数此方法要显示的默认消息。 Can be a function created by jQuery.validator.format(value). 可以是由jQuery.validator.format(value)创建的函数。 When undefined, an already existing message is used (handy for localization), otherwise the field-specific messages have to be defined. 如果未定义,则使用已经存在的消息(便于本地化),否则必须定义特定于字段的消息。

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

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