jQuery validation fails when putting step=“1” in a “datetime-local” input type

对着背影说爱祢 提交于 2020-01-14 10:12:26

问题


I'm using https://jqueryvalidation.org/ form validation plugin

When I put step="1" in a datetime-local input field to show seconds, if I'm validating that field with the plugin (just "required", for example), I get a javascript error saying "Step attribute on input type datetime-local is not supported". If I put this step="1" in a field that is not being validated, it works without problems.

$("#testingform").validate();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.min.js"></script>
<form id="testingform">
    <input type="datetime-local" step="1" name="date1" required>
    <button type="submit">Send</button>
</form>

... just put a date and time WITH SECONDS, and click 'Send' button. You can see the error in the console.

I've already opened a bug in the github page of the project, but maybe any of you had the same problem and have found a way to solve it or at least avoid it for the moment until they fix it.


回答1:


jQuery validate automatically apply rules on input type, so step cannot be validated.

This will help you:

$("#testingform").validate({
  rules: {
    date1: {step: false}
  }
});
<form id="testingform">
    <input type="datetime-local" step="1" name="date1" required>
    <button type="submit">Send</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.min.js"></script>


来源:https://stackoverflow.com/questions/46484679/jquery-validation-fails-when-putting-step-1-in-a-datetime-local-input-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!