Regular expression for MM / DD / YYYY in Javascript

I just wrote this regular expression in javaScript, but it doesn't seem to work, here is my function:

function isGoodDate(dt){ var reGoodDate = new RegExp("/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/"); return reGoodDate.test(dt); } 

and so I call it in my validation form

 if(!isGoodDate(userInput[1].value)){ alert("date not in correct format of MM/dd/YYYY"); return false; } 

Now I want it to return MM / DD / YYYY, however, if I enter a valid date in it, is there a warning? Any ideas anybody?

+8
source share
6 answers

Try the following:

 function isGoodDate(dt){ var reGoodDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/; return reGoodDate.test(dt); } 

You either declare a regex with:

 new RegExp("^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$") 

Or:

 /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/ 

Pay attention to /

+12
source

Perhaps because you declare the isGoodDate() function and then you call the isCorrectDate() function?

Try:

 function isGoodDate(dt){ var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/; return reGoodDate.test(dt); } 

Works like a charm, test it here .

Please note that this regular expression will check dates from 01/01/1900 to 31/12/2099 . If you want to change the borders of the year, change these numbers (19|20) on the last block of regular expressions. For instance. If you need the year ranges from 01/01/1800 to 31/12/2099 , just change it to (18|20) .

+3
source

I do not think you need a regular expression for this. Try the following:

 function isGoodDate(dt){ var dts = dt.split('/').reverse() ,dateTest = new Date(dts.join('/')); return isNaN(dateTest) ? false : true; } //explained var dts = dt.split('/').reverse() // ^ split input and reverse the result // ('01/11/2010' becomes [2010,11,01] // this way you can make a 'universal' // datestring out of it ,dateTest = new Date(dts.join('/')); // ^ try converting to a date from the // array just produced, joined by '/' return isNaN(dateTest) ? false : true; // ^ if the date is invalid, it returns NaN // so, if that the case, return false 
+2
source

I agree with @KooiInc, but not enough to check for NaN

 function isGoodDate(dt){ var dts = dt.split('/').reverse() ,dateTest = new Date(dts.join('/')); return !isNaN(dateTest) && dateTest.getFullYear()===parseInt(dts[0],10) && dateTest.getMonth()===(parseInt(dts[1],10)-1) && dateTest.getDate()===parseInt(dts[2],10) } 

which will handle the 29/2/2001 and 31/4/2011


For this script to handle US dates run

 function isGoodDate(dt){ var dts = dt.split('/') ,dateTest = new Date(dt); return !isNaN(dateTest) && dateTest.getFullYear()===parseInt(dts[2],10) && dateTest.getMonth()===(parseInt(dts[0],10)-1) && dateTest.getDate()===parseInt(dts[1],10) } 
+2
source

Add this to your code and it works great here. click here http://jsfiddle.net/Shef/5Sfq6/

 function isGoodDate(dt){ var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/; return reGoodDate.test(dt); 

}

+1
source

Confirm (DD-MM-YYYY) format :)

 function isGoodDate(dt) { var reGoodDate = /^(?:(0[1-9]|[12][0-9]|3[01])[\-.](0[1-9]|1[012])[\-.](19|20)[0-9]{2})$/; return reGoodDate.test(dt); } 
0
source

Source: https://habr.com/ru/post/890859/


All Articles