coure2011
coure2011

Reputation: 42404

why custom validator method not firing?

I have created a custom validation, which I want to use to compare 2 select boxes, but the additonal method is not firing not sure how to fix it? here is the jsfiddle: http://jsfiddle.net/ughCm/97/ On change of select value it is not firing the 'comaremodelyear' method, even not on .valid().

here is the code

$.validator.addMethod("comparemodalyear", function(value, element, param) {
    alert('h');
    return true;    
}, "From year must be greater!");

$(document).ready(function() {
    $('#savebtn').click(function() {
        var test = $("#experiment").valid();
    });

});

html code:

<form id="experiment" action="/" method="post">
    <input type="text" required name="fine" />
<select comparemodalyear name="testingA">
    <option value="1">Val1</option>
    <option value="2">Val2</option>
</select>
    <button id="savebtn" type="button">Save</button>
</form>

Upvotes: 0

Views: 193

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to add it as a class not as an attribute

<select class="comparemodalyear" name="testingA">
    <option value="1">Val1</option>
    <option value="2">Val2</option>
</select>

then

$.validator.addClassRules('comparemodalyear', {
    comparemodalyear: true
});

Demo: Fiddle


If you don't want to pass a custom value to the rule, then there is no need for addClassRules() because the addMethod() makes an internal call to addClassRules() based on method.length < 3 condition, where method.length is the count of arguments to the validate method

$.validator.addMethod("comparemodalyear", function (value, element) {
    alert('h');
    return true;
}, "From year must be greater!");

Demo: Fiddle

Upvotes: 1

Related Questions