问题描述:

Hi I am using MVC 3 with Razor, I am using the below code.

I need to know whether I can use Annotations with this input tag?

 <input type="text" id="@endDateName" name="@endDateName" value="@String.Format("{0:MM/dd/yyyy}", endDateValue)" />

I need annotation to check whether a proper date time value has been entered and not any random text.

Thanks in advance

After some help from Amyz,

I have used date picker now, and yes it does not allow characters like a,..z,[email protected]#, and so on.

But the problem is it does allow a date like say 02/1212312321/1231231313 this is what I want to prevent now the same can be seen on http://jqueryui.com/demos/datepicker/

网友答案:

Its better to handle this at client side only: for that you can use date picker:

Date Picker

or you can use data annotation like:

[DataType(DataType.Date, ErrorMessage = "Please enter a valid date (ex: 2/14/2011)")]

public DateTime DateTime { get; set; }

The final step is to register the adapter in our global.asax file:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DataTypeAttribute), typeof(DataTypeAttributeAdapter));
相关阅读:
Top