2

I'm new to MVC3 - and cannot figure out why the Date format validation is not working on client-side i.e., if I manually change the text box and input an invalid format date. I'm using jQuery date picker for this field. The strange thing is that the Required Field Validation is working.

Can someone please let me know what's wrong with this code?

Thanks!

VIEWMODEL

    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date, ErrorMessage="Please enter a valid date in the format dd/mm/yyyy")]
    [Required]
    public DateTime ADate { get; set; }

VIEW

        @Html.TextBox("ADate", Model.ADate.ToShortDateString(), new { @class = "ADate" })
        @Html.ValidationMessageFor(model => model.ADate, "*")

2 Answers 2

1

DataType and DisplayFormat only works when using DisplayFor or EditorFor, and not with a TextBox. You can't set a class name with EditorFor, however.

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

Comments

1

You can use underlying TextBoxFor() and specify a class name

@Html.TextBoxFor(model => model.ADate, new { @class = "ADate" }) 

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.