-4

I want to add a jquery piece of code where i had to chck if the expirydate month and year input fields have been defined and if yes, verify if they are greater than today's date

if ($("#ccm").length) {
            errormsg = "Date should more than todsay";
            $("#form").closest('.form-group').addClass('has-error');            
            error = true;                   
        }
uBobby
  • 45
  • 1
  • 6

1 Answers1

2

Dates are kinda problematic in JavaScript. Most common approach is to grap a framework that handles time related stuf like moment.js.

You need to ensure the format, that the user can input this date. Therefore it would be a good approach to split it up into three select boxes (Month, Day, Year). So you can always expect the input to be in the same format.

Next step will be to grap the current time with moment's now()

var now = moment();

After that you need to compare the time the user entered to now. Therefor the next step is to turn the users input into a moment

var userInput = moment(inputYear + "-" + inputMonth + "-" + inputDay);

Notice: The format you are passing in here is related to your chosen timezone inside moment.js. Then you could go on and compare the two moments.

now.diff(userInput);

So your final code could look like

...
var now = moment();
var userInput = moment(inputYear + "-" + inputMonth + "-" + "inputDay);
if(now.diff(userInput)) {
   // your code here
}
...

Hope this is helpful to you!

Tstar
  • 518
  • 5
  • 24