[RESOLVED]How to make my 3 drpdownlist as a required fields

Hi, i added in my register view 3 dropdownlist to selecte the date of birth ( DAY  , MONTH, YEAR)

 @Html.DropDownList("DateOfBirthDay", Enumerable.Range(1, 31).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }), "-- Select Day --")
                @Html.DropDownList("DateOfBirthMonth", Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i) }), "-- Select Month --")
                @Html.DropDownList("DateOfBirthYear", Enumerable.Range(1900, 109).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }), "-- Select Year --")

and i add them in my controller

// POST: /Account/Register
        public async Task<ActionResult> Register(string DateOfBirthDay , string DatehOfBirthMonth, string DateOfBirthYear, RegisterViewModel model)

but i would love to make them as a required files and if they are not, to return the form..

how to do?

Easiest way to validate a dropdown list is to check if the element contains a non-null value:

change button-type from submit to button

@using (Html.BeginForm())
  ... form elements here ...

  <input id="submitBtn" type="button" value="Submit" />

Then handle the on-click event to check if valid, then submit if validation passes

  $("#submitBtn").on("click", function () {
    var canSubmit = true;
    if ($("#DateOfBirthDay").val() == "") {
      canSubmit = false;
      alert = "Please select a day";

    if ($("#DateOfBirthMonth").val() == "") {
      canSubmit = false;
      alert("Please select a month";

    if ($("#DateOfBirthYear").val() == "") {
      canSubmit = false;
      alert("Please select a year";

    if (canSubmit) {

Alternatively, if these are model properties (DateOfBirthDay, DateOfBirthMonth, DateOfBirthYear), then you could give each a "[Required]" annotation in the model, and use a DropDownFor helper with a ValidationMessageFor helper message:

@Html.DropDownListFor(model => model.DateOfBirthDay, ...)
@Html.ValidationMessageFor(model => model.DateOfBirthDay)

using this method, it basically takes care of itself.

Not directly related to your question, but have you considered using a DatePicker. It provides better user experience and also solves your issues. You can use a JQuery UI DatePicker or some other DatePicker

Demo of JQueryUI DatePicker http://jqueryui.com/datepicker/

Integrating this in MVC

ASP.NET MVC 3: Integrating with the jQuery UI date picker and adding a jQuery
validate date range validator

Leave a Reply