Javascript validation february 31

I am trying to write code that will validate form data. I have a date field, which should be in the format mm/dd/yyyy . I needed to catch exceptions like February 31st, so I added this code:

 var d = new Date(dob); if (isNaN(d.getTime())) { //this if is to take care of February 31, BUT IT DOESN'T! error = 1; message += "<li>Invalid Date</li>"; } else { var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/; var validFormat = date_regex.test(dob); if (!(validFormat)) { error = 1; message += "<li>Invalid date format - date must have format mm/dd/yyyy</li>"; } } 

However, I found something very strange: while the date of 02/32/2000 errors as an invalid date, 02/31/2000 does not work!

+8
source share
4 answers

Due to what I said in the comments ...

Another way to check if the date is correct is to check if the material you passed to the new Date function is passed, like what comes out of it, for example:

 // Remember that the month is 0-based so February is actually 1... function isValidDate(year, month, day) { var d = new Date(year, month, day); if (d.getFullYear() == year && d.getMonth() == month && d.getDate() == day) { return true; } return false; } 

then you can do this:

 if (isValidDate(2013,1,31)) 

and it will return true if it is valid and false if it is invalid.

+30
source

Can I use the library?

My first port for handling dates in Javascript moment.js : "A javascript date library for parsing, validating, managing, and formatting dates.

+1
source

The method of transferring the string name "mm / dd / yyyy" is to create a date object and verify that its month and date match the entered data.

 function isvalid_mdy(s){ var day, A= s.match(/[1-9][\d]*/g); try{ A[0]-= 1; day= new Date(+A[2], A[0], +A[1]); if(day.getMonth()== A[0] && day.getDate()== A[1]) return day; throw new Error('Bad Date '); } catch(er){ return er.message; } } 

isvalid_mdy ('02 / 31/2000 ')

/ * return value: (Error) Bad date * /

+1
source

After the destruction of my head .getMonth() Date .getMonth() (as well as weekday by .getDay() ), which is 0-index (despite the fact that the year, day and all the others are not like ... oh my god. ..) I rewrote Jeff's answer to make it more readable and more convenient for those who use the method from the outside.

ES6 Code

You can name the passing month as 1-indexed as you usually expect.

I parsed the input using the Number constructor, so I can use strict equality for a more confident comparison of values.

I use UTC version methods to avoid having to deal with the local time zone.

In addition, I have broken the steps into some variables for readability.

 /** * * @param { number | string } day * @param { number | string } month * @param { number| string } year * @returns { boolean } */ function validateDateString(day, month, year) { day = Number(day); month = Number(month) - 1; //bloody 0-indexed month year = Number(year); let d = new Date(year, month, day); let yearMatches = d.getUTCFullYear() === year; let monthMatches = d.getUTCMonth() === month; let dayMatches = d.getUTCDate() === day; return yearMatches && monthMatches && dayMatches; } 
+1
source

All Articles