javascript - jQuery validation custom method didn't work -
I've added a method to the jQuery validator like this
$ .validator.addMethod ('My aqual', function (value, element) {return value == element.value; // it does not work here}, 'Please enter more years!'); $ .metadata.setType ("attr", "valid"); Validate $ ("#EfirmForm") ({showErrors: function (error map, error list) {this.defaultShowErrors ();}, error placement: function (error, element) {error.appendTo (element.parent ("td AddClass ("success");}, / / rule: {txt_end: {expected: true, ("TD")) /} success: function (label) {label.text ("OK! ") MyEqual:" #txt_begin "}}, submitHandler: function () {}});
The form looks like
& Lt; Td width = "200" & gt; Input type = "text" name = "txt_begin" id = "txt_begin" size = "8" maxlength = "4" class = "expected year ui-widget-content ui-corner-all" /> & Amp; Nbsp; From & amp; Nbsp; & Lt; Input type = "text" name = "txt_end" id = "txt_end" size = "8" maxlength = "4" class = "expected year ui-widget-content ui-corner-all" / & gt; & Lt; / TD & gt; & Lt; Td width = "200" & gt; & Lt; / Td> & Lt; / TR & gt; & Lt; TR & gt; & Lt; Td colspan = "2" & gt; & Lt; Input type = "submit" name = "btn_submit" id = "btn_submit" value = "submit" category = "ui-button ui-state-default ui-corner-all" /> & Lt; Input type = "button" name = "btn_cancel" id = "btn_cancel" value = "cancel" class = "ui-button ui-state-default-UI-corner-all" /> & Lt; / TD & gt; & Lt; TD & gt; & Nbsp; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; / Form & gt; & Lt; / Div & gt;
But why do not I do the work that I have added?
return value == element.value; // It did not work here
It always came true for any value, am I missing something here? I did not use the method created because later in the form I would need to type another method to check for greater or equal and less or equal ("> =" and "& lt; =") Is tested more or "==" by changing it with "> =" or "& lt; =" is equal to or equal to or equal to
Reading your code, I think with your custom method, you can compare two text inputs Oshis are, you have a parameter for its legalization: To do this, you will need to handle the third parameter of the callback function: myEqual: "#txt_begin" / Code>.
$ Validator.addMethod ('myEqual', function (value, element, absolute) {// will return the ultimate value == $ (the ultimate) .val (); // is}, 'message'); // "#txt_begin"
Comments
Post a Comment