How do I get the input to automatically parse the numbers to follow the format [MM / YY] like it is done in this codepen example?
Right now in order for the validation to work, you need to manually enter 5 digits, including a '/' separator entered manually by the user:
$("#ExpDate").on("input", function() {
var expdate = $(this).val();
if (expdate.length == 5) {
checkExpDate($(this));
} else {
notCorrectInput($(this));
removeError($(this));
}
if (expdate.indexOf("\\") > -1) {
$(this).val(expdate.replace("\\", "/"));
expdate = expdate.replace("\\", "/");
}
if (expdate.length == 2 && expdate.indexOf("/") > -1) {
$(this).val("0" + expdate);
}
});
function checkExpDate($ele) {
var ExpirationInput = (function() {
var maximumLength = 4;
var selector;
var createExpirationInput = function(mainSelector) {
selector = mainSelector;
$(selector).keypress(function(e) {
$(selector).removeClass("has-error");
if (shouldProcessInput(e, maximumLength, selector)) {
var inputValue = getInputValue(e, selector);
if (inputValue.length >= 2) {
var newInput = inputValue.slice(0, 2) + " / " + inputValue.slice(2);
$(selector).val(newInput);
} else {
$(selector).val(inputValue);
}
}
});
};
var parseExpirationInput = function(expirationSelector) {
var inputValue = getNumber($(expirationSelector).val());
var month = inputValue.slice(0, 2);
var year = "20" + inputValue.slice(2);
return {
year: year,
month: month
};
};
return {
createExpirationInput: createExpirationInput,
parseExpirationInput: parseExpirationInput
};
})();
var exdate = $ele.val();
var dateregex = /^(0[1-9]|1[0-2])\/?([0-9]{2})$/;
if (exdate.length != 0) {
if (!dateregex.exec(exdate)) {
activateError($ele);
$ele.next().text("Date is not valid.");
} else {
var today = new Date(),
someday = new Date();
someday.setFullYear("20" + exdate.substr(3, 4), exdate.substr(0, 2), 1);
if (someday < today) {
activateError($ele);
$ele.next().text("Card is expired.");
} else {
correctInput($ele);
removeError($ele);
}
}
} else {
correctInput($ele);
removeError($ele);
}
}
$("#ExpDate").on("input", function() {
var expdate = $(this).val();
if (expdate.length == 5) {
checkExpDate($(this));
} else {
notCorrectInput($(this));
removeError($(this));
}
if (expdate.indexOf("\\") > -1) {
$(this).val(expdate.replace("\\", "/"));
expdate = expdate.replace("\\", "/");
}
if (expdate.length == 2 && expdate.indexOf("/") > -1) {
$(this).val("0" + expdate);
}
});
$("#ExpDate").keydown(function(e) {
var exdate = $(this).val();
if (exdate.length == 5 && keyIsNum(e)) {
e.preventDefault();
return false;
}
});
$("#ExpDate").keydown(function(e) {
if (
(e.keyCode > 64 && e.keyCode < 91) ||
(e.keyCode > 185 && e.keyCode < 191)
) {
e.preventDefault();
return false;
}
});
function correctInput($ele) {
$ele.addClass("input-correct");
}
function notCorrectInput($ele) {
$ele.removeClass("input-correct");
}
function removeError($ele) {
$ele.next().removeClass("active");
$ele.removeClass("input-error");
}
function activateError($ele) {
$ele.addClass("input-error");
$ele.removeClass("input-correct");
$ele.next().addClass("active");
}
function keyIsNum(event) {
console.log(event);
if (
(event.keyCode >= 48 && event.keyCode <= 57) ||
(event.keyCode >= 96 && event.keyCode <= 105)
) {
return true;
} else {
return false;
}
}
function checkIfEmpty($ele) {
if ($ele.val().length == 0) {
$ele.addClass("input-error");
return false;
} else {
return true;
}
}
.input-error:focus {
outline: none;
}
.input-error {
border-color: #DB4F4F !important;
background-color: #FEF8F8 !important;
}
.input-correct {
border-color: #75CC72 !important;
background-color: #F9FEF8 !important;
}
.help-info {
display: none;
}
.help-info.active {
display: block;
}
span {
display: block;
font-size: 13px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="box">
<input type="text" id="ExpDate" placeholder="MM / YY" />
<span class="help-info"></span></div>
</div>