简体   繁体   English

如何使用只读的HTML5验证字段客户端?

[英]How can I validate a field client-side using HTML5 that is read only?

I have an input field that launches a datepicker. 我有一个输入字段,用于启动日期选择器。 To make this work on a mobile device, I've made the field read only. 为了使这项工作在移动设备上,我已将该字段设为只读。 This means that I can launch a date picker on click (or tap) using jQuery, and the tap doesn't open the keyboard on a mobile device. 这意味着我可以使用jQuery点击(或点击)启动日期选择器,并且点击不会在移动设备上打开键盘。

However - a readonly attribute bypasses the HTML5 validation mechanisms. 但是 - readonly属性绕过HTML5验证机制。 Can I add something that then either mimics the HTML5 validation look and feel, to keep consistency, or that forces HTML5 validation despite the field being readonly ? 我可以添加一些东西,然后模仿HTML5验证的外观和感觉,保持一致性,或强制HTML5验证,尽管该字段是readonly

Edit : I'm looking for validating that the field is filled in (required) and preferably that it fits a certain date format. 编辑 :我正在寻找验证该字段已填写(必填),并且最好是符合某种日期格式。

readonly fields cannot have the required attribute, generally they will already contain some value. readonly字段不能具有required属性,通常它们已经包含一些值。

Look at the HTML5 Standard Drafts 查看HTML5标准草案

Try looking at the 'pattern' attribute. 尝试查看'pattern'属性。

For dates, this pattern might come in handy: 对于日期,这种模式可能派上用场:

^\d{4}\-\d{1,2}\-\d{1,2}$|^\d{1,2}\/\d{1,2}\/\d{4}$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM