asp.net mvc - Validate DateFormat In Mvc -
i have property expireddate define in mvc
[required] [displayformat(dataformatstring = "{0:mm/dd/yyyy}")] public datetime? expirationdate { get; set; } i want validate if date on page not in correct format. format of date using mm/dd/yyyy.
you should include datatype attribute datatype.date. these can both found in system.componentmodel.dataannotations namespace.
[required] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:mm/dd/yyyy}")] public datetime? expirationdate { get; set; } this answer includes more attributes.
updated include instructions enable client-side validation in asp.net mvc4
to enable client-side validation, need this:
add jquery.validation plugin footer
<%: scripts.render("~/scripts/jquery.validate.min.js") %> <%: scripts.render("~/scripts/jquery.validate.unobtrusive.min.js") %>add web.config
<appsettings> <add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" /> </appsettings>use css when using @html.validationmessagefor() initally hidden , displays via javascript validation
<style type="text/css"> /* styles validation helpers */ .field-validation-error { color: #e80c20; font-weight: bold; } .field-validation-valid { display: none; } input.input-validation-error { border: 1px solid #e80c20; } input[type="checkbox"].input-validation-error { border: 0 none; } .validation-summary-errors { color: #e80c20; font-weight: bold; font-size: 1.1em; } .validation-summary-valid { display: none; } </style>
Comments
Post a Comment