简体   繁体   中英

How to check validation input date field

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>

这是我的两个领域的图片

My js:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM