0

Can someone explain to me why this is not hitting my helper function? Not sure if it's a syntax thing or what..

jQuery.validator.addMethod("expDateExistInPast", function (value) {
    return customFunction(value);
}, "Expiration cannot exist in past.");

$("form[name='myForm']").validate({
 rules: {
    exp: {expDateExistInPast: true}
 }, 
 messages: {
     exp: {
        required: "message", 
        expDateExistInPast: "otherMsg"
    }
 }, 
  submitHandler: function (form) {
       form.submit();
  }
})


function customFunction(value) {
    alert("magic here");
}

UPDATE - Adding HTML

<input class="myClass" type="text" name="expiration" id="exp" maxlength="4" pattern="^(0[1-9]|1[0-2])[0-9][0-9]$" required="" placeholder="MMYY" aria-required="true" aria-invalid="true">
2
  • You'll need to show us the relevant HTML, because everything in your OP is working perfectly fine for me: jsfiddle.net/se4dc18h Commented Oct 4, 2016 at 18:46
  • added HTML above. just a little info on this, I'm expecting a MMYY date value from the user. Hence the regex. Commented Oct 4, 2016 at 18:59

1 Answer 1

1

Your input contains name="expiration"...

<input class="myClass" type="text" name="expiration" id="exp" maxlength="4" pattern="^(0[1-9]|1[0-2])[0-9][0-9]$" required="" placeholder="MMYY" aria-required="true" aria-invalid="true">

However, the problem is that your .validate() method is using exp within the rules object...

rules: {
    exp: {
        expDateExistInPast: true
    }
},

You must only use the name attribute here...

rules: {
    expiration: { // <- this is the NAME of the input
        expDateExistInPast: true
    }
},

Working: jsfiddle.net/se4dc18h/

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

Comments

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.