Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  118] [ 6]  / answers: 1 / hits: 16372  / 16 Years ago, thu, march 19, 2009, 12:00:00

I have some code for validating date below:




function validateForm() {
var errFound = 0;
//var patt_date = new RegExp(^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$);
var patt_date = new RegExp(^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$);
if (patt_date.test(document.getElementById(datefrom).value) == false){errFound = errFound + 1;document.getElementById(datefrom).className = error;}

if (errFound > 0)
alert('Please correct red colored field!');
else
return true;
return false;
}




Above code should work with YYYY-MM-DD format, but fail to validate date such as 2009-02-29



The commented code should work (//var patt_date = new RegExp...), it can catch 2009-02-29,
but it ruin the validation when i put invalid data and try to correct it, it keeps complain there something wrong with form value after i had correct them (especially on form with multiple input)



Maybe someone can fix the current regex?



Edited, what i want just a simple replacement for above regexp, mean a new regexp pattern not the whole new method to validate date

And for reference, i simply grab the regexp pattern from:

http://www.regexlib.com/REDetails.aspx?regexp_id=694 and

http://www.regexlib.com/REDetails.aspx?regexp_id=933



Tested with 2009-02-29, 1st link work & 2nd not. Again the problem was only the 2nd regexp didn't detect value 2009-02-29 as invalid while 1st can (but it ruin my code? so it's must be there something wrong with it).



Thanks,

Dels


More From » regex

 Answers
7

Don't do the whole date validation with a regular expression, that's really pushing the limits of what regexps were designed for. I would suggest this procedure instead:




  1. Check date against regexp /^d{4}-d{2}-d{2}$/

  2. Extract year, month, and day using substr() and convert to integers

  3. Use some if statements to validate the integers. Like so:




if (month == 2) {
if (day == 29) {
if (year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {
// fail
}
}
else if (day > 28) {
// fail
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
// fail
}
}
else {
if (day > 31) {
// fail
}


(That could certainly be written more concisely) Alternatively, you could probably perform this validation using Javascript's Date class - you might have to do something like parsing the date, converting it back to a string, and checking if the two strings are equal. (I'm not a Javascript expert)


[#99824] Friday, March 13, 2009, 16 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
lucianod questions
;