0

How to validate Datetime property in my MVC2 Validation.

I have telerik datetime picker to validate

<%= Html.Telerik().DatePicker().Name("EffectiveDate")%>

 [Required(ErrorMessage = "EffectiveDate Must not be empty!")]
            public DateTime EffectiveDate { get; set; }
        }

if I use this code I am not able to validation the EffevtiveDate. Because I am just suspecting that effective date showing as "" instead of null.

how to check "" value using regular expressiong here?

thanks

2 Answers 2

1

Try making your model property a nullable DateTime:

[Required(ErrorMessage = "EffectiveDate Must not be empty!")]
public DateTime? EffectiveDate { get; set; }

Then the [Required] attribute should be enough to validate against an empty string.

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

Comments

1

You could try:

[Required(AllowEmptyStrings = false)]
public DateTime EffectiveDate { get; set; }

Comments

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.