ASP.Net MVC 2 Model Validation Regex Validator fails
Asked Answered
L

2

2

I have following property in my Model Metadata class:

[Required(ErrorMessage = "Spent On is required")]
[RegularExpression(@"[0-1][0-9]/[0-3][0-9]/20[12][0-9]", 
   ErrorMessage = "Please enter date in mm/dd/yyyy format")]
[DataType(DataType.Date)]
[DisplayName("Spent On")]
public DateTime SpentOn { get; set; }

But whenever I call ModelState.IsValid it always returns false because regex is not validating. I have matched the entered date (08/29/2010) against new regex using same pattern and it matches perfectly.

What am I doing wrong?

Lightening answered 29/8, 2010 at 11:15 Comment(0)
S
2

That's because regex applies to strings and not DateTime properties. If the user enters an invalid string which cannot be parsed to a DateTime instance from the model binder it will add a generic error message before your regex pattern executes.

You have a couple of possibilities:

  1. Customize the error message in a resource file
  2. Write a custom model binder
  3. Use a string property (I feel guilty for proposing this :-))
Schulze answered 29/8, 2010 at 11:19 Comment(1)
aha, @Darin it is converting DateTime to String and that is not giving it MM/dd/yyyy thanks dear! I'll develop new ValidationAttribute, because there are some other validation also needed on this field.Lightening
M
4

Actualy there is another workaround for this. You can simply subclass the RegularExpressionAttribute

public class DateFormatValidatorAttribute : RegularExpressionAttribute {
    public DateFormatValidatorAttribute()
        : base(@"[0-1][0-9]/[0-3][0-9]/20[12][0-9]") 
        {
            ErrorMessage = "Please enter date in mm/dd/yyyy format";
        }

        public override bool IsValid(object value) {
            return true;
        }
}

in your Global.asax.cs on application start register the RegularExpression addapter for client side validation like so:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof(DateFormatValidatorAttribute), 
                typeof(RegularExpressionAttributeAdapter));

Now you get to have the build-in MVC regular exression validator client side and keep the DateTime as your property type

Manrope answered 24/11, 2010 at 15:31 Comment(0)
S
2

That's because regex applies to strings and not DateTime properties. If the user enters an invalid string which cannot be parsed to a DateTime instance from the model binder it will add a generic error message before your regex pattern executes.

You have a couple of possibilities:

  1. Customize the error message in a resource file
  2. Write a custom model binder
  3. Use a string property (I feel guilty for proposing this :-))
Schulze answered 29/8, 2010 at 11:19 Comment(1)
aha, @Darin it is converting DateTime to String and that is not giving it MM/dd/yyyy thanks dear! I'll develop new ValidationAttribute, because there are some other validation also needed on this field.Lightening

© 2022 - 2024 — McMap. All rights reserved.