Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top