randy
randy

Reputation: 1877

JQuery CreditCard validator extend

I am using Jquery Validator My challenge is when the user is updating their profile info. but not changing their CC .

When in the update page i only show 4*****1111

So the validation fails if use: (code snippet below)

$("#profile-form").validate({
      rules: {
             ccnum: { required: true, creditcard: true },
             exp_year: { ccDate: true},
             exp_month: { ccDate: true}
             }

But if the user enters a new CC number it does what it is suppose to.

So my problem what can i do if the user wants to update something else and leave the CC card un-touched.

Any ideas/suggestions?

Upvotes: 1

Views: 1916

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

I would write a new rule that calls the credit card validation method if the element's value has changed:

$.validator.addMethod("creditcard-custom", function (value, element) {
    return this.optional(element) || 
        element.value === element.defaultValue ||
        $.validator.methods.creditcard.call(this, value, element);
}, $.validator.messages.creditcard);

Usage:

$("#myform").validate({
    rules: {
        ccnum: {
            required: true,
            'creditcard-custom': true
        },
        exp_year: {
            ccDate: true
        },
        exp_month: {
            ccDate: true
        }
    }
});

Example: http://jsfiddle.net/andrewwhitaker/kqczf/2/

Upvotes: 1

Related Questions