1

I am trying to use the jQuery Credit Card Validator to validate credit cards.

The basic usage is given as

$('#cc_number').validateCreditCard(function(result)
{
alert('CC type: ' + result.card_type.name
  + '\nLength validation: ' + result.length_valid
  + '\nLuhn validation: + result.luhn_valid');
});

I looked on the demo JS file included on that site and couldn't make head nor tail.

What I am trying to achieve is onkeyup of input, do something depending on what card type is caught:

 //on key up of input
 if (card == valid) 
   {
     if (card == visa)
      {
        //do something
      }
     else if (card == mastercard)
     {
        //do something
     }
       // repeat for rest of card types
   }
 else
   {
     //Just print an error
   }

I know it's fairly basic stuff, but can anybody help me with how to achieve?

 my HTML:
 <input type="text" id="cc_number" />
0

3 Answers 3

3

Developer of jQuery Credit Card Validator here.

jCCV binds the keyup event so you don’t need to do it. (actually it’s a little more complicated than that — all you need to know is that every time the value of the field changes, your callback function is executed).

$('#cc_number').validateCreditCard(function(result)
{
    // this will execute everytime the value of the `#cc_number` field changes

    if (result.length_valid && result.luhn_valid) {
        if (result.card_type.name == 'visa') {
            // do something
        } else if (result.card_type.name == 'mastercard') {
            // do something
        }       
        // repeat for rest of card types
    } else {
        // just print an error
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

No problem! If you need any more help, you can contact me directly on Twitter @PawelDecowski. Glad to help!
Pawel, Please help on this post @ stackoverflow.com/questions/22148771/…
1

try something like this:

$("#cc_number").on("keyup", function() {
    $(this).validateCreditCard(function(result) {
    alert('CC type: ' + result.card_type.name
      + '\nLength validation: ' + result.length_valid
      + '\nLuhn validation: ' + result.luhn_valid);
    });

    if (result.card_type.name) {
      if (result.card_type.name == visa)
      {
         //do something
      }
      else if (result.card_type.name == mastercard)
      {
         //do something
      }
      // repeat for rest of card types
   }
   else {
     //Just print an error
   }
});

1 Comment

Actually this is wrong. jCCV automatically binds the necessary events so the callback is executed whenever the value of the field changes. It will be optional in an upcoming version.
0

Use this one by Stripe: https://github.com/stripe/jquery.payment

Much better.

1 Comment

This is a bit of an overkill since it’s a payment library and the validation functions are just helpers. I’m biased, though, as I’m the jQuery Credit Card Valildator developer ;)

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.