Quack
asked on
I need to validate a date field with MM/DD/YY requirements using a regex
I need to validate a date field with MM/DD/YY requirements using a regex within a cold fusion form.
ASKER
returning an error saying 01/01/17 is an invalid format
oops. Read YYYY... Then its:
or
(0[1-9]|1[0-2])\/(0[1-9]|[1-2]\d|3[0-1])\/\d{2})
or
(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])\/([0-9]{2})
Do you want to match "01" or "01" and "1"
By YY do you mean a two digit year or a 4 digit year?
For now I Am going to assume you want to match Both, and have a 4 digit year.
MM/DD/YYYY:
By YY do you mean a two digit year or a 4 digit year?
For now I Am going to assume you want to match Both, and have a 4 digit year.
MM/DD/YYYY:
^(0?[1-9]|1[1-2])\/(0?[1-9]|[1-2][0-9]|3[0-1])\/(000[1-9]|00[1-9][0-9]|0[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$
ASKER
MM/DD/YY is 2 digit month, 2 digit day and 2 digit year
oh see you only want the two digit and only accept a 0 padding, so this is all you need.
^(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0-1])/[0-9][0-9]$
Although, since you are using DATES, you probably want to weed out the ones which are invalid based on the month of the year (30 days has september .... and all that)
IE you DO NOT want to consider February 30th or31st to be a valid date.
Nor do you want June 31st, April 31st, November 31st or September 31st.
This amended Regex will accomplish this:
Edit: Slightly simplified the above regex to this:
IE you DO NOT want to consider February 30th or31st to be a valid date.
Nor do you want June 31st, April 31st, November 31st or September 31st.
This amended Regex will accomplish this:
^(02/([1-2]0|[0-2][1-9])|((0[1,3,4,5,7,8,]|1[0,2])/([1-3]0|[0-2][1-9]|31))|((0[4,6,9]|11])/([1-3]0|[0-2][1-9])))/[0-9][0-9]$
Edit: Slightly simplified the above regex to this:
^((0[1-9]|1[0-2])/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])/30|(0[1,3,5,7,8]|1[0,2])/31)/[0-9][0-9]$
ASKER
does this prevent the input of a future date by chance? I'm assuming not...
ASKER
that regex when used is kicking me straight to the server side validation...I believe it has something to do w/ the / vs \ separators w/in the regex...can something else be used vs / ?
No, and while we could limit it to doing so using a lot more coding you would have to update it every day...
Not only that but since you are using a two digit year, you would need to know what the oldest possible valid date to enter is.
IE if someone entered 01/21/23 do they mean 2013 (future! abort!) or 1923 (Past! OK! ) No way of knowing for certain with a two digit date.
If it's low hanging fruit we could easily change it to only allow 00 through 17, and then you would only be able to enter future date of the current year, and you would only have to update the regex every year.
Likewise we could go to the current month and you would have to update it every month and year (already getting much more complex too, and you have to remember to fix the substitutions -- I set them equal for now and you would have to remember to amend them appropriately as the months changed)
Not only that but since you are using a two digit year, you would need to know what the oldest possible valid date to enter is.
IE if someone entered 01/21/23 do they mean 2013 (future! abort!) or 1923 (Past! OK! ) No way of knowing for certain with a two digit date.
If it's low hanging fruit we could easily change it to only allow 00 through 17, and then you would only be able to enter future date of the current year, and you would only have to update the regex every year.
^((0[1-9]|1[0-2])/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])/30|(0[1,3,5,7,8]|1[0,2])/31)/(0[1-9]|1[0-7])$
Likewise we could go to the current month and you would have to update it every month and year (already getting much more complex too, and you have to remember to fix the substitutions -- I set them equal for now and you would have to remember to amend them appropriately as the months changed)
^(((0[1-9]|1[0-2])/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])/30|(0[1,3,5,7,8]|1[0,2])/31)/(0[1-9]|1[0-6])|((0[1-8]|0[1-8])/([1-2]0|[0-2][1-9])|(0[1,3-8]|0[1,3,8])/30|(0[1,3,5,7,8]|0[1,3,5,7,8])/31)/17) )$
that regex when used is kicking me straight to the server side validation...I believe it has something to do w/ the / vs \ separators w/in the regex...can something else be used vs / ?
If you are having problems with the "/" might need to escape them (normally you don;t but sometimes you do.)
Original:
^((0[1-9]|1[0-2])\/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])\/30|(0[1,3,5,7,8]|1[0,2])\/31)\/[0-9][0-9]$
Change Yearly:^((0[1-9]|1[0-2])\\/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])\\/30|(0[1,3,5,7,8]|1[0,2])\\/31)\\/[0-9][0-9]$
Change Monthly:^(((0[1-9]|1[0-2])\/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])\/30|(0[1,3,5,7,8]|1[0,2])\/31)\/(0[1-9]|1[0-6])|((0[1-8]|0[1-8])\/([1-2]0|[0-2][1-9])|(0[1,3-8]|0[1,3,8])\/30|(0[1,3,5,7,8]|0[1,3,5,7,8])\/31)\/17) )$
ASKER
yeah...the 2 digit year is killing the whole thing...I'm going to attempt to change their postion to accept MM/DD/YYYY...would your original regex supplied earlier in this thread suffice for that? I tested it and it look to validate on the regex testing site
Oops, in above on "change Yearly" I re-posted the original with accidental extra slashes, here was the real updated change yearly:
^((0[1-9]|1[0-2])\/([1-2]0|[0-2][1-9])|(0[1,3-9]|1[0-2])\/30|(0[1,3,5,7,8]|1[0,2])\/31)\/(0[1-9]|1[0-7])$
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hey Quack,
Glad to help! :)
Glad to help! :)
Open in new window
orOpen in new window