javascript - jQuery Validation conditional dependency: ensure text input matches value if radio button checked -
I have some form elements that adhere to this type of format:
& Lt; Input type = "radio" name = "test" value = "a" & gt; & Lt; Input type = "text" size = "3" name = "weight A" id = "A" & gt; & Lt; Br> & Lt; Input type = "radio" name = "test" value = "b" & gt; & Lt; Input type = "text" size = "3" name = "weight-B" id = "b" & gt; & Lt; Br> I am using jQuery validation plugin to perform client-side validation. What I would like to do with these areas, to ensure that the text input corresponding to the selected radio button is equal to 100. I have implemented it successfully on server-side using PHP, but would like to add a JS method to give immediate feedback. Before I submit the form, I already have a jmt range: rule in numeric range [1-100] Within two text areas included to limit user input. How do I do this work? Can jQuery.validator.addMethod be the way to do this? Edit: In response to the comments of the participant, I have tried an addMethod: Val () === "" "" "" ""; Var selB = $ ('[name = "test"]'). Val () === "B"; if (sella and company ($ ("#a") .val ()! = = "100" )) {Return false;} and if (selB & amp; amp; amp; ($ ("($" ("#b"). Val ()! == "100")) {return false;} And back to true;}, "the selected option should be equal to 100."); It starts triggering verification for # a but Not #b , and display error message Message: is specified by the rule, which is specified by addMethod . Please note that I have a minimum programming background. < P> Try it out:
Demo: $ Validator.addMethod ("selectWt", function (value, element) {if ($ (element) .prev (). (': Check')) {return ($ (element) .val () === 100 );} And {return true;}}, "the selected option will be equal to 100."); This rule can be implemented normally, it only checks to see if the previous radio element of the element is checked if yes , Then it returns true , if the value of the text element is 100 . The way it is written, it only works when your type = text element immediately follows the type = radio element if If you change the HTML system, then it will need to be tweaked.
This can be made as a parameter by passing in the 100 value to be more flexible. Demo:
$ Validator.addMethod ("selectWt", function (value, element, absolute) {if ($ (element) .prev (). (': Check')) {return ($ (element) .val () === Ultimate );} And {return true;}}, "selected option equals {0}."); Validate ... $ ('# myform') (Start the {// plugin rule: {myfield: {selectWt: 100 / Use a parameter instead of "true"}}}});
Comments
Post a Comment