Credit card expiry date with slash problem

Credit card expiration date: mm/yy Whenever I enter two numbers, I need add "/", but currently when I delete the third number, he will only delete one number. How can I delete the third number along with the slash?

Please help thank you.

var characterCount
$('#expiry').on('input', function(e) {
  if ($(this).val().length == 2 && characterCount < $(this).val().length) {
    $(this).val($(this).val() + '/');
  }
  characterCount = $(this).val().length
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="expiry" type="text" />



Read more here: https://stackoverflow.com/questions/68470650/credit-card-expiry-date-with-slash-problem

Content Attribution

This content was originally published by Chieh YU WU at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: