检查HTML5表单的有效性的方法? [英] a way to check validity of HTML5 forms?
问题描述
是否可以根据我为其设置的模式检查html5表单的输入元素是否有效?我知道psuedo类的东西..但我希望像:
document.getElementById('petitionName')。有效
可以返回 true
或 false
..
Is it possible to check if an input element of an html5 form is valid based on the pattern I set for it? I know the psuedo class stuff.. but i'm hoping something like:
document.getElementById('petitionName').valid
can return true
or false
..
我真的希望我不需要创建javascript来重新验证这些东西。
I really hope I don't need to create javascript to re-verify this stuff.
推荐答案
有两种检查有效性的方法。
there are two ways to check the validity.
-
inputElement.checkValidity()
返回true
或
false
-
inputElement.validity
返回有效性状态对象inputElement.validity.valid
返回true / false
inputElement.checkValidity()
returnstrue
orfalse
inputElement.validity
returns the validity-state object.inputElement.validity.valid
returnstrue/false
而不是使用keyup,您也可以使用'input'事件。所有实现了约束验证API的浏览器也实现了输入事件。
Instead of using keyup, you can also use the 'input' event. All browser, which have implemented the constraint validation API, have also implemented the input-event.
如果您使用的是1. Opera在此处有一个错误,并显示其验证暗示。所以你应该使用2.
If you are using 1. Opera has a bug here and will show its validation hint. So you should use 2.
我已经创建了一个html5表单libary,它实现了所有未知功能,无法浏览器+修复HTML5浏览器中的问题。所以一切都符合规范中的定义,但它建立在jquery ....(http://afarkas.github.com/webshim/demos/index.html)之上。
I have created a html5 forms libary, which implements all unknown features to incapable browsers + fixes issues in HTML5 browsers. So everything works like defined in the spec, but it's build on top of jquery.... (http://afarkas.github.com/webshim/demos/index.html)
这篇关于检查HTML5表单的有效性的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!