views:

311

answers:

2

Hello, I have run into a problem trying to modify a form I myself have not built.

The form has a asp:input field for a date value, wich is checked by a requiredFieldVal and a rangeVal. The rangeVal has type set to "date" and min value 2000-01-01: max value 3000-01-01

Now to the problem, I'm trying to add so that the user also can input the date in the form of "20000101" in other words without the "-".

I tried adding another rangeVal with Integer type and the min,max values, and put them both in the same ValidationGroup, but that didnt work.

How do I allow the user to use both (either one of them) formats in the date input field.

Thank you in advance!

A: 

You can use a regular expression validator in place of the range validator. I'm a little rusty with Regex, but I am sure you can find something here.

http://www.regular-expressions.info/

Here is a link to the dates page:

http://www.regular-expressions.info/dates.html

try this, I think it will work. If not, let me know and I will work on correcting it:

((19|20)\d\d[- /.](0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])|((19|20)\d\d(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])))

Another post brought up a point this regex won't validate some invalid dates like feb 31. In this case, I would just create a validation class which inherits from the regex validator and override the EvaluateIsValid method and check to see if it is an actual date. This allows most of the validation done on the client end, with the backup being at the server level which should be doing a secondary validation anyway.

Kevin
A very basic mask would be ^\d{4}[-]?\d{2}[-]?\d{2}$ due to date validation, trying to do all this in Regex would lead to a crazy, hard to understand expression.
Garry Shutler
Thanks for the suggestion, thou my Regex skills is at bare minimum, and modifying their example to my needs can be a bit tricky
Andreas
Seems to work fine from my inital testing, thanks a bunch Kevin. Now i will just make sure that the date without the "-" gets accepted as a valid date in the DB
Andreas
The regular expression posted does not check that a date is valid. For example: 1999-02-31 passes this regular expression's validation, but is invalid because February 31 is not a real date and the question said that dates should be in the range of 2000-01-01 and 3000-01-01. I maintain that a custom validator is a more robust approach.
dariom
+2  A: 

When you use multiple validation controls to validate a single control all of the validation controls must pass.

You could use a regular expression (as Kevin points out) but doing the validation you require (checking that the value is a valid DateTime object and within the specified range) will be difficult to do cleanly.

I'd suggest writing your own CustomValidator control and then use that to validate your values however you want to.

dariom