如果输入不正确,HTML5输入类型日期有时为空值 [英] HTML5 input type date is sometimes empty value if entered incorrectly

查看:104
本文介绍了如果输入不正确,HTML5输入类型日期有时为空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入类型为date的东西很疯狂.
这是我的html:

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>

如果输入无效值,它将自动更改为有效值.
但并非总是如此.最长日期始终为31.

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

例如:
输入:"01/55/2018" =>"01/31/2018"-True
输入:"02/55/2018" =>"02/31/2018"-错误

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

如果为False,则jquery或php均无法获取该值.
它总是返回".

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

推荐答案

来自文档

value属性(如果指定且不为空)必须具有一个值这是有效的日期字符串.

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.

这篇关于如果输入不正确,HTML5输入类型日期有时为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆