0

I want to validate credit card using jquery, Limit 16 numbers, automatically add a space after 4 numbers when typing numbers. The credit card number like

4444 4444 4444 4444

How to validate like this using jquery

Sparky
  • 94,381
  • 25
  • 183
  • 265
jemin
  • 11
  • 3

1 Answers1

0

If you want to do this with just jQuery, and not card.js (though it seems pretty cool) this code should take care of it:

$('.credit').on('input', function(){
    var number = $(this).val();
    number = number.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1').trim().substring(0, 19);
    $(this).val(number);
});

Help from How to insert space every 4 characters for IBAN registering? and Javascript trim a string to length

Community
  • 1
  • 1
ivywit
  • 413
  • 1
  • 7
  • 16