0

I created a mask for input date field, but when someone type yearFrom in this format dd.mm.yyyy(28.45.2020), so I need when user switch to another field yearTo it need to showed him an error on input field yearFrom (Your date format is wrong) or something else.

<div class="col-md-6">
  Year_from
  @Html.TextBoxFor(model => model.yfrom, new { @class = "form-control", maxlength = "10", minlength="4" })
</div>
<div class="col-md-6">
  Year_to 
  @Html.TextBoxFor(model => model.yto, new { @class = "form-control", maxlength = "10", minlength = "4" })
</div>

This is picture of my two fields

My js:

<script type="text/javascript">
  $(document).ready(function () {
    $('#yfrom').mask('00.00.0000');
    $('#yto').mask('00.00.0000');
  });
</script>
emppeak
  • 158
  • 12

1 Answers1

0

It seems it cannot be done easily without writing a custom funciton to handle your date format or using a third-party library like Moment.js. This thread should help you write a proper function to handle your date format if you want it that way.

However I would opt for the use of a library since parsing the date by hand is quite cumbersome. With Moment.js you will be able to write something like moment(dateFromUser, 'DD.MM.YYYY', true).isValid() to check if the date provided by the user is fine.

Quacke
  • 253
  • 1
  • 4