3

I have a ViewModel with a String property and the following Data Annotation :

Edit to work with string

[DataType(DataType.Date, ErrorMessage="Not Working !!!")]
public String StringBirthDate1 { get; set; }

That's my view

@Html.EditorFor(model => model.StringBirthDate1 )
@Html.ValidationMessageFor(model => model.StringBirthDate1)

If I run my application and put an invalid Date like '---' or 29.02.1900 I don't get any validation error !

3 Answers 3

5

Ok I've given up trying to use built-in MVC tools for data validation !

I did a custom Validation Attribute :

public class ValidDateStringAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        DateTime dtout;
        if (DateTime.TryParse(value.ToString(), out dtout ))
        {
            return true;
        }
        return false;
    }
}

Here is my View Model decorated with the custom attribute :

[ValidDateString(ErrorMessage="Invalid date format")]
public String BirthDate1 { get; set; }

Works like a charm :-)

Sign up to request clarification or add additional context in comments.

Comments

2

It seems to me, that [DataType(DataType.Date, ErrorMessage="Not Working !!!")] working when it attached to string property. Try to use:

[DataType(DataType.Date, ErrorMessage="Not Working !!!")]
puplic string StringBirthDate1{get;set;}

public DateTime BirthDate1 
{ 
    get{return DateTime.Parse(StringBirthDate1);} 
    set{StringBirthDate1 = value.ToString();} 
}

3 Comments

I have tried as you say and I have no better results ... Sorry.
You are right I've changed it to string but It's not validating my date anymore.
Thanks for your answer even if it didn't solve my problem it drove me the right way :-)
0

I didn't like any of the solutions I found so I kept poking at possibilities until I came up with one I do like. I added a regular expression validator utilizing the regular expression from this article: http://answers.oreilly.com/topic/226-how-to-validate-traditional-date-formats-with-regular-expressions/

    [Required(ErrorMessage = "Birthdate is required. [MM/DD/YYYY]")]
    [RegularExpression(@"^([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[- / .](1[9][0-9][0-9]|2[0][0-9][0-9])$", ErrorMessage = "Birthdate must be in MM/DD/YYYY format.")]
    public Nullable<DateTime> Birthdate { get; set; }

The result is, if the field is blank I get the required error message and if anything is in the field, but it is not a valid date, I get the regular expression message.

I might add that it seems very silly that [DataType] doesn't accept an error message. I tried exactly like the original author of this thread. That would have been logical and intuitive.

2 Comments

It's a interesting way for validation but it will not keep you from getting unmanaged exception e.g. 31st of february
Good call Arno. The error message becomes "The field ____ must be a date." for an entry of 2/31. I'll see about updating the regular expression this week to correct for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.