0

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>
1
  • Use this in class like <select class="comparemodalyear" name="testingA"> Commented Mar 17, 2015 at 6:15

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

From ur fiddle I see u have also added $('#form').validate(), Can I call this multiple times? Its a dynamic form and I need to call this on click of button every time, is it safe?
@coure2011 you can ignore that... not really required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.