开发者

How to match a value to a string within "rules" using jquery validate.js (bassistance.de)

开发者 https://www.devze.com 2023-03-21 19:11 出处:网络
I want to check if a form radio input value matches a string exactly, something like \"equalTo\" but just to match a string and not anoth开发者_运维技巧er field input but can\'t find if there is a sim

I want to check if a form radio input value matches a string exactly, something like "equalTo" but just to match a string and not anoth开发者_运维技巧er field input but can't find if there is a simple way to do this similar to using "equalTo".

As below I want the form field "foo" value to match "bar" exactly and was hoping to replace the ????????s with a simple command if this is possible?

If not I would really appreciate some help showing how I can implement a string match within the "rules" method.

$("#this_form").validate({
rules:{
            name:{
                 required:true
                 },
             foo:{
                 ????????: "bar",
                 required:true
                 }
        }

});

So here I want the form to only validate if the value of foo is "bar" and not if the value is "manchu".

< input type="radio" name="foo" value="bar" />

< input type="radio" name="foo" value="manchu" />

(I think the reason is pretty obvious, the answer to foo needs to be bar otherwise I do not want the form to submit.)


You can do something like this:

  jQuery.validator.addMethod("myFunc", function(val) {
    if(val=='myString'){
      return true;
    }
    return false;
  }, "Values do not match.");

You can then use this method 'myFunc' like you would use the built in email function.

...
foo:{
     myFunc:true
0

精彩评论

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