开发者

How can I validate a form field value against a variable with jQuery Validate?

开发者 https://www.devze.com 2023-04-04 21:43 出处:网络
I\'m sure there must be a simple solution to this.. I simply want to create a rudimentary human verificat开发者_运维问答ion tool for an online form. In pseudo-code, something like

I'm sure there must be a simple solution to this..

I simply want to create a rudimentary human verificat开发者_运维问答ion tool for an online form. In pseudo-code, something like

$answer = "foo";

if (form['question'] == $foo){
    // Proceed
} else {
    // Fail
}

The jQuery docs seem to have an equalTo method but this is to compare a form field with another form value..

Any tips greatly appreciated! :)


You need to have a look at JQuery Validation plugin at http://docs.jquery.com/Plugins/validation. Consider the following example:

How can I validate a form field value against a variable with jQuery Validate?

You may change the rules as explained in the plugin to implement your custom validations.


Have you looked at this plugin? You can use it to make fields required, validate for certain value types (number, string, credit card, etc), and I believe you can write callback functions to validate for specific values.

http://docs.jquery.com/Plugins/validation


var answer = 'foo';
if ($('#question').val() == answer) {
  // Proceed
} else {
  // Fail.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消