的HTML5输入模式属性不能在表格外工作? [英] html5 input pattern attribute not working outside a form?

查看:117
本文介绍了的HTML5输入模式属性不能在表格外工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此小提琴可以正常工作-当用户输入无效的国家/地区代码时,它将显示警告.

没有form元素的其他小提琴不起作用.似乎输入的pattern属性需要一个form进行验证.顺便说一句,我正在做一个没有表单的复杂页面,我想用`pattern验证我的输入字段.有办法吗?

解决方案

这是因为验证是HTML5 表单验证( http://jsfiddle.net/y66vH/3/

this fiddle works as intended - it displays a warning when the user enters an invalid country code.

This other fiddle, without the form element, doesn't work. It seems the input's pattern attribute needs a form to validate. By the way, I'm doing a complex page without forms and I' d like to validate my input fields with `pattern. Is there a way to do that?

解决方案

This is because the validation is part of the HTML5 form validation (http://www.w3.org/TR/html5/forms.html#client-side-form-validation). The validations are triggered when the form is submitted en when there are any errors, the submit will be cancelled.

To trigger it manually, use the checkValidity() function on the element:

$('input').blur(function(evt) {
    evt.target.checkValidity();
}).bind('invalid', function(event) {
   alert('oops');
});

http://jsfiddle.net/y66vH/3/

这篇关于的HTML5输入模式属性不能在表格外工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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