Click here to Skip to main content
16,017,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How can i validate a date field in mvc?
we have the [Required], but I want to validate the date field for a specific range.

thanks
Posted

 
Share this answer
 
v2
Comments
Jijutj 8-Oct-13 2:42am    
I want to do it in mvc
Sushil Mate 8-Oct-13 2:48am    
check updated links.
Jijutj 8-Oct-13 2:54am    
thanks.
Sushil Mate 8-Oct-13 2:55am    
wc
C#
if (person.Name.Trim().Length == 0)
    {
        ModelState.AddModelError("Name", "Name is required.");
    }

http://msdn.microsoft.com/en-us/library/dd410404(v=vs.90).aspx[^]
http://msdn.microsoft.com/en-us/library/ee256141(v=vs.100).aspx[^]

Please go through this link.
 
Share this answer
 
v2
Comments
Jijutj 8-Oct-13 2:45am    
thanks for the link, but i need to validate a date field, whether it is in a specified range, say for example it should be in the range 1/1/1990 to 31/12/2013.
Gitanjali Singh 8-Oct-13 2:52am    
[Required]
[Display(Name = "Start DateTime")]
[RegularExpression("^(([0-2]?[0-9]|3[0-1])/([0]?[1-9]|1[0-2])/[1-2]\d{3}) (20|21|22|23|[0-1]?\d{1}):([0-5]?\d{1})$", ErrorMessage = "Invalid date")]
public string DateTimeStart { get; set; }

you can try regular expression for specified range.
Jijutj 8-Oct-13 3:12am    
it shows Invalid date for all dates.
Member 11873377 31-Jul-15 11:06am    
how to validate a date i mean the given date must be in prior to the current date. it should not exceed the current date

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900