I recently started learning AngularJS and decided to create a credit card validator. I successfully implemented the Luhn Algorithm in a custom filter, but now I'm facing issues with validating the expiration date as well. The conditions for a valid expiration date are as follows: - 08/16 - 02/2015 - 0518 - the date must not be expired.
Since I discovered that Angular already has a built-in date filter, I attempted to create one. However, my code doesn't seem to work as expected. Here's what I have:
/**
* validate-expiry-date Module
*
* Validates the date format and ensures it is not in the past
*/
angular.module('validate-expiry-date', []).filter('validDate', [function () {
return function (date) {
var currentDate = new Date();
var month, year, day;
if (/^\d{2}\/\d{2}$/.test(date)) {
month = date.substring(0, 2);
year = 20 + date.slice(-2);
day = new Date(year, month);
return(currentDate > day);
}if (/^\d{2}\/\d{4}$/.test(date)) {
month = date.substring(0, 2);
year = date.slice(-4);
day = new Date(year, month);
return(currentDate > day);
}else if (/^\d{4}$/.test(date)) {
month = date.substring(0, 2);
year = 20 + date.slice(-2);
day = new Date(year, month);
return(currentDate > day);
};
}
}])
Can anyone help me understand what might be wrong with my code? Thanks, B.