views:

225

answers:

1

I want to use Data Annotations to validate DateTime fields, but I'm running into problems. According to documentation on MSDN, the following should do the job

[Range(typeof(DateTime), "1/2/2004", "3/4/2004",
    ErrorMessage = "Value for {0} must be between {1} and {2}")]

However, this marks any date I enter as invalid!

At first I thought it was not picking up UK dates (when I tried 26/2/2004) but I can't even get it to use dates such as 2/2/2004.

I'm using the dataannotations within MVC2, and using the MicrosoftAjax framework for clientside validation.

Any suggestions?

Thanks

+1  A: 

As far as i know the RangeAttribute can only validate number on client side, you'll have to write a custom javascript validator for this to work...

check out http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx for an example on how to do this.

moi_meme
My problem at the moment is that I can't get it working on the server side! The client side is something that I have to sort out once the server side is working.
Alex White
Well, it works for me, how do you display your field, in order for your attributes to work i think you must use Html.EditorFor or Html.Editor method
moi_meme