1

I asked this question previously while trying to use datepicker to validate a date in asp.net mvc 4.5.1. Now, I have eliminated datepicker and am just trying to validate a date to mm/dd/yyyy format using data annotations in the model and regex to force the format. Here is my code:

Model:
        [DisplayName("Start Date")]
        [DataType(DataType.Date, ErrorMessage = "Date not valid.")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        [RegularExpression(@"^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$", ErrorMessage = "Date must be in format mm/dd/yyyy")]
        public DateTime? DT_Project_Start { get; set; }

View:

<div class="form-group">
       @Html.LabelFor(model => model.DT_Project_Start, new { @class = "control-label col-md-2 CreateEditFieldNamesSpan" })
<div class="col-md-10">
       @Html.TextBoxFor(model => model.DT_Project_Start)
       @Html.ValidationMessageFor(model => model.DT_Project_Start)
                            </div>
                        </div>

Doesn't work...validation works perfectly while IN the form, but when I click "submit" it says invalid date NO MATTER what, even if the date is valid and in the right format. Does anyone know what I am missing?

24
  • Because your property is DateTime. Delete the regex (its pointless). You need to modify the $.validator method or use jquery.globalize.js Refer this answer if your using the jquery-ui datepicker Commented Nov 16, 2015 at 5:51
  • Thanks for the response. If I remove regex, how do I force the client to type in 4-digit year? I am not using datepicker at all at this point. It didn't work with IE. Commented Nov 16, 2015 at 6:55
  • Firstly the [DataType] and [DisplayFormat] are a bit pointless if you use @TextBoxFor(). Second you should consider asking a question about the datepicker and IE since there is no reason it should not work. Commented Nov 16, 2015 at 7:21
  • Also, if you wanting MM/dd/yyyy, why are you allowing a - as the date separator? Commented Nov 16, 2015 at 7:25
  • I am really new to asp.net and just learning everything. I have spent a lot of hours trying to get this. Someone gave me the regex and I didn't remove the - in the code, but I will do that! I did have a thread about datepicker, and I got it to work (except for in IE), but I still have to allow for manual input, so still need the 4-digit year to be forced, either way. If @TextBoxFor() doesn't work with [DataType] and [DisplayFormat], what should I use? Commented Nov 16, 2015 at 7:49

1 Answer 1

1

If you want to force the user to enter a 4 digit date, you can use the following RegularExpressionAttribute to your property

[RegularExpression(@".*\d{4}.*", ErrorMessage = "Please enter a 4 digit year")]
public DateTime? DT_Project_Start { get; set; }

Note that a DateTime property includes a time component which is why your current regex could fail. Note also that you do not need the [DataType] and [DisplayFormat] attributes. Those are only respected when you use the EditorFor() or DisplayFor() methods. Since your using TextBoxFor(), you can format the date using

@Html.TextBoxFor(m => m.DT_Project_Start, "{0:MM/dd/yyyy}")

to strip the time component from the display.

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

5 Comments

This is working really well so far. Thank you so much for the time and responses that you've given. My votes don't seem to count yet, but as soon as I get a high enough reputation, I'll come back and vote this up! You've been very helpful!
Do you think that I can add a generic datepicker function to this without validation for ease of entry of the date?
Sorry, Not sure what you mean by your last comment.
We ultimately would like to have the drop-down calendar on these date fields because it makes entry easier, but they still need to be able to type in the date because dates can be waaaaaay in the future, so I still need the validations. Still need the 4-digit year validation so the type the correct millennium.
The regex attribute will still work with a datepicker control - for example jquery-ui datepicker

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.