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=""></script>
<input id="expiry" type="text" />

Read more here:

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: