Skip to content Skip to sidebar Skip to footer

How Do I Validate A Credit Card Expiry Date With Javascript?

I'm a complete Javascript beginner and I'm really stuck on my assignment. I have to get the user to enter credit card details and validate them with Javascript. Here are my problem

Solution 1:

And for the credit card expiration validation you can do like this.

var today, someday;
var exMonth=document.getElementById("exMonth");
var exYear=document.getElementById("exYear");
today = new Date();
someday = new Date();
someday.setFullYear(exYear, exMonth, 1);

if (someday < today) {
   alert("The expiry date is before today's date. Please select a valid expiry date");
   return false;
}

Solution 2:

In the following code you have some deviations

var firstName=document.getElementById("firstName");
var lastName=document.getElementById("lastName");
var email=document.getElementById("email");
var postcode=document.getElementById("postcode");
var paymentType=document.getElementById("paymentType");
//here why did you use .value. Probably removing .value would fix your issue
var exMonth=document.getElementById("exMonth").value;
var exYear=document.getElementById("exYear").value;
var cardNumber=document.getElementById("cardNumber").value;

change the last three lines to something like this

var exMonth=document.getElementById("exMonth");
var exYear=document.getElementById("exYear");
var cardNumber=document.getElementById("cardNumber");

Solution 3:

I found a problem with the previous answer. I'm posting here my tested solution.

The problem with the previous answers they are not considering the day. For solve it you must use the last day of the month. To find it in Javascript you can use setFullYear(year, month, day) where the day is zero.

Bonus: month in javascript is 0 - 11

But when you set a month you shouldn't use month - 1.

Ex: setFullYear(2020, 6, 0) // -> 31 Jun 2020.

const today = new Date();
const ed = new Date();
ed.setFullYear(year, month, 0);

if (ed < today) {
}

Solution 4:

I'm doing something very similar. The answers provided didn't work for my credit card expiry validation, but this one did. (I have used your variables)

if(exMonth.selectedIndex<month && exYear.selectedIndex<=year)
{
    alert("Please enter a valid expiration date");
    return false;
}

Solution 5:

Use inputmask and moment.

lets say you want a 10 year date range from today's date dynamically...

include the jQuery.InputMask.js and moment.js library (CDN or self hosted)...

Optional: CDN

https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/bindings/inputmask.binding.js
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js

HTML

<input type="text" id="expires_mmyy" name="cc_expires" maxlength="5" />

JS

$('#expires_mmyy').inputmask({
   alias: 'datetime', 
   inputFormat: 'mm/yy'
   min: moment().add(1, 'M').format('MM/YY'),
   max: moment().add(10, 'Y').format('MM/YY')
})


Post a Comment for "How Do I Validate A Credit Card Expiry Date With Javascript?"