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

4
  • You can use jquery masking, have a look at this: jquery-plugins.net/tag/masked-input Commented Jul 13, 2016 at 5:34
  • Take a look at card.js at this: jessepollak.github.io/card Commented Jul 13, 2016 at 5:36
  • Using jquery mask, 4444 4444 4444 4444, how to limit 16 numbers Commented Jul 13, 2016 at 5:38
  • Please do not use the jquery-validate tag when the question contains nothing about this plugin. Edited. Thanks. Commented Jul 13, 2016 at 14:05

1 Answer 1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.