Check out the sample I have here:
Here's the HTML code:
<input type="text" name="card_number" id="card_number" class="disbaled_cc required-input valid" maxlength="16" placeholder="xxxx-xxxx-xxxx-xxxx">
And this is the JS code:
function cardFormat(){
var format_card = $(this).val().replace(/(\d{4})(\d{4})(\d{4})(\d{4})/, "$1-$2-$3-$4");
if ($(this).val() == '' || $(this).val().match(format_card) || $(this).val().length == 0) {
console.log("invalid");
}else{
console.log("valid");
}
}
$("#card_number").on('blur change', function () {
cardFormat();
});
I'm looking to modify the user input in a specific format like so:
For instance, when the user types this:
1234567891091234 ---> 1234-5678-9109-1234
If the user enters and formats correctly, it should remain as such:
1234-5678-9109-1234 ---> 1234-5678-9109-1234
In this scenario, the issue arises with having 19 characters (max should be 16). How do you suggest we solve this challenge?
If anything is unclear, please let me know for further clarification.
Could you provide feedback on the functionality of my code?
Thank you in advance!