获取HTML5号码输入的值 [英] Getting the value of a HTML5 number input

查看:119
本文介绍了获取HTML5号码输入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用其中一种新的HTML5输入类型,



我认为您使用默认值0来确保如果没有输入无效的东西,那么这个领域就没有了,因为似乎没有明显的方法来区分这两者。



在阅读验证规范和一些测试(在Opera 10.54中)后,我得出结论:

 < input id =emailtype =emailvalue =blah> 
document.getElementById(email)。validity.typeMismatch // True

Doesn'适用于< input type =number> 。不确定它是否应该,或者它是否正在进行中。但是该属性确实存在,但它总是返回False。



阅读更多 2



您还可以设置自定义验证方法参考 3


I'm using one of the new HTML5 input types, number:

<input type="number" />

In Opera, which is the only desktop browser I know of which currently recognises it, it renders it as such:

The problem I'm facing is one of validation. If a user types something invalid into the field, eg: "abc", the value returned by myInput.value is an empty string. This makes it impossible to tell whether the user left the field blank, or if they entered some incorrect data. Is there any way to get the real value of the field?

解决方案

The HTML5 draft defines:

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

Reference1

I suppose you'd have use a default value of "0" to make sure the field was left untouched or if something invalid was entered, since there seems to be no obvious way to differentiate the two.

After reading up validation specs and some testing (in Opera 10.54) I concluded that:

<input id="email" type="email" value="blah">
document.getElementById("email").validity.typeMismatch // True

Doesn't work on <input type="number">. Not sure if it's supposed to, or if it's a work in progress. The property does however exist, though it always returns False.

Read more2

You can also set a custom validation method Reference3

这篇关于获取HTML5号码输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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