Is there any way to validate a DateTime property in a model using a regular expression,
or do regular expression validators only apply to properties of type string?
For example in the model below I never can get MyBirthDate to validate, but I can do so with MyStreet. I am assuming that the MyBirthDate property needs to be a string. Is that true?
public class MyModel
{
[RegularExpression("some expression for a date")]
public DateTime MyBirthDate {get; set;}
[RegularExpression("Some Expression for a street address")]
public string MyStreet { get; set; }
}
Server side validation of dates is automatically performed by mvc 3 by using all stadard formats of dates allowed by the culture settings of the server. Unluckly client side validation is not provided automatically for dates. A solution is using regular expressions...but
since they validates just one culture you must set a fixed culture in the globalization tag of your web.config...this way you are sure of what culture is set on the server and you can provide a regular expression for the same culture....because the regular
exppression will be applied just on the client side...while on the server side you will still have mvc 3 automatic validation. A better solution would be to use a client globalization library, reflect server side culture on he client side and adding a client
validation provider for dates...you can find this solution implemented in the mvc controls toolkit.
Thanks for responding to my query. I apologize for not including the regular expressions in my example - I wasn't looking for the regurlar expression but rather trying to find out if these work with a DateTime property since it is not a string. That seems
to be the case since the other two replies indicate that to be so. In fact, I am already using the same regex for dateTime as you've included here. Must be a good one. Cheers!
You might be interested to see this post, your problem is solved there:
Thanks for responding to my query. You are the first to confirm that Regular Expressions only apply to strings (which makes sense.) And I have looked at the article you referenced and may try to apply it - but will still be needing to perform the client
side validation, but maybe that is already handled by a jQuery date-picker.
Thanks for your response to my query. I've learned that my hunch was correct - Regular Expressions only apply to strings. And I've learned that MVC 3 takes care of the server side validation. Now all I need is to figure out how I will implement the client
side validation. Your answer hinges on being closest to what I need - but I'm going to have to look at the mvc control toolkit to be able to figure it out. I don't know how long that will take me.
If you want a rapid solution you can use yhe regular expression it works only on th client side since on the client side the content of a field is always a string ...also when it will be transformed in a datetime on the server. if you use the datepicker it
parse the string and do an acceptable attempt to force the user to insert only a regular daye however some user actions may allow the user to insert a wrong fate....so it i noy 100% client side validation but just an help. However the jquery picker has also
a globalization module that you can synvhronize with the culture of the server as explained in this post of my blog: http://dotnet-programming.com/post/2011/12/14/Globalization-Validation-and-DateNumber-Formats-in-AspNet-MVC.aspx
n8nt
Member
61 Points
29 Posts
DateTime validation
Jun 26, 2012 03:41 AM|LINK
public class MyModel { [RegularExpression("some expression for a date")] public DateTime MyBirthDate {get; set;} [RegularExpression("Some Expression for a street address")] public string MyStreet { get; set; } }bkis1112
Participant
1061 Points
226 Posts
Re: DateTime validation
Jun 26, 2012 03:57 AM|LINK
Validate email:
[RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", ErrorMessage = "Email Invalid")]Validate dateTime: dd/MM/yyyy
and more ..., you can see and apply in your apps http://www.regular-expressions.info/
Thanks for reading my post
Trần Lê Thành Trung
Pupuru
Member
380 Points
65 Posts
Re: DateTime validation
Jun 26, 2012 03:58 AM|LINK
Hi, from what I know [RegularExpression] in model applies only to string. If you're using it for DateTime, it will be simply ignored.
You might be interested to see this post, your problem is solved there:
Lead Business Process Analyst for CFM System Project
Colliers International
francesco ab...
All-Star
20944 Points
3286 Posts
Re: DateTime validation
Jun 26, 2012 07:45 AM|LINK
Mvc Controls Toolkit | Data Moving Plug-in Videos
n8nt
Member
61 Points
29 Posts
Re: DateTime validation
Jun 27, 2012 01:51 AM|LINK
Thanks for responding to my query. I apologize for not including the regular expressions in my example - I wasn't looking for the regurlar expression but rather trying to find out if these work with a DateTime property since it is not a string. That seems to be the case since the other two replies indicate that to be so. In fact, I am already using the same regex for dateTime as you've included here. Must be a good one. Cheers!
n8nt
Member
61 Points
29 Posts
Re: DateTime validation
Jun 27, 2012 01:54 AM|LINK
Thanks for responding to my query. You are the first to confirm that Regular Expressions only apply to strings (which makes sense.) And I have looked at the article you referenced and may try to apply it - but will still be needing to perform the client side validation, but maybe that is already handled by a jQuery date-picker.
n8nt
Member
61 Points
29 Posts
Re: DateTime validation
Jun 27, 2012 02:00 AM|LINK
Thanks for your response to my query. I've learned that my hunch was correct - Regular Expressions only apply to strings. And I've learned that MVC 3 takes care of the server side validation. Now all I need is to figure out how I will implement the client side validation. Your answer hinges on being closest to what I need - but I'm going to have to look at the mvc control toolkit to be able to figure it out. I don't know how long that will take me.
Thanks
francesco ab...
All-Star
20944 Points
3286 Posts
Re: DateTime validation
Jun 27, 2012 02:50 AM|LINK
Mvc Controls Toolkit | Data Moving Plug-in Videos