HTML5 input type date is sometimes empty value if entered incorrectly

99封情书 提交于 2021-02-09 08:17:34

问题


There is a crazy thing with input type date.
Here is my html:

function getValue() {
  $('#entered').text($('input[name=test_date]').val());  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" min="2018-01-01" max="2018-05-05" name="test_date">
<button onclick="getValue()">Get value</button>
<p>Entered: <span id="entered"></span></p>

If I enter an invalid value, it will change to valid value automatically.
But not always true. The max date is always 31.

Ex:
Enter: "01/55/2018" => "01/31/2018" -- True
Enter: "02/55/2018" => "02/31/2018" -- False

And In case False, both jquery or php cannot get the value.
It always return "".


I want to get value even if it's invalid.
Do you have any suggestion?

==================
Problem resolved

According to http://crbug.com/231811, the reason is simplicity.

If you want to show error message when User enter invalid date.
You can validate with input.validity.badInput or validationMessage


回答1:


From documentation HTML Living Standard

The value attribute, if specified and not empty, must have a value that is a valid date string.

The value sanitization algorithm is as follows: If the value of the element is not a valid date string, then set it to the empty string instead.



来源:https://stackoverflow.com/questions/50285254/html5-input-type-date-is-sometimes-empty-value-if-entered-incorrectly

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