延迟 HTML5:无效伪类直到第一个事件发生
我最近发现 :invalid
伪类在页面加载后立即应用于 required
表单元素。例如,如果您有以下代码:
<style>
input:invalid { background-color: pink; color: white; }
input:valid { background-color: white; color: black; }
</style>
…
<input name="foo" required />
那么您的页面将加载一个空的粉红色输入元素。在 HTML5 中内置验证固然很棒,但我认为大多数用户并不希望表单在有机会输入任何值之前进行验证。有什么方法可以延迟伪类的应用,直到影响该元素的第一个事件(表单提交、模糊、更改,任何适当的)?没有 JavaScript 可以做到这一点吗?
I recently discovered that the :invalid
pseudo-class applies to required
form elements as soon as the page loads. For example, if you have this code:
<style>
input:invalid { background-color: pink; color: white; }
input:valid { background-color: white; color: black; }
</style>
…
<input name="foo" required />
Then your page will load with an empty pink input element on it. Having validation built in to HTML5 is great, but I don't think most users expect the form to validate before they've had a chance to enter any values at all. Is there any way to delay the application of the pseudo-class until the first event affecting that element (form submit, blur, change, whatever's appropriate)? Is it possible to do this without JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
http://www.alistapart.com/articles/forward-thinking-form -验证/
按照这个逻辑,您的代码将看起来像这样...
在这里创建了一个小提琴: http://jsfiddle.net/tbERP/
正如您所猜测的,并且您将从小提琴中看到,此技术仅在元素具有焦点时显示验证样式。一旦您将焦点移开,样式就会被删除,无论它是否有效。无论如何都不理想。
http://www.alistapart.com/articles/forward-thinking-form-validation/
Following this logic, your code would look something like this...
Created a fiddle here: http://jsfiddle.net/tbERP/
As you'd guess, and as you'll see from the fiddle, this technique only shows the validation styling when the element has focus. As soon as you move focus off, the styling is dropped, regardless of whether it is valid or not. Not ideal by any means.
这些答案已经过时了。现在,您可以通过使用 CSS 检查占位符伪类来完成此操作。
它以正常背景开始,当您输入不完整的电子邮件地址时,它会变成粉红色。
These answers are out of date. Now you can do this by checking for a placeholder pseudo-class with CSS.
It starts with a normal background and turns pink as you enter you incomplete email address into it.
这在纯 CSS 中是不可能的,但可以使用 JavaScript 来完成。这是一个 jQuery 示例:
This is not possible in pure CSS, but can be done with JavaScript. This is a jQuery example:
这是 kzh 的 答案
This is a VanillaJS (no jQuery) version of kzh's answer
Mozilla 用自己的
:-moz-ui-invalid
伪类,仅适用于与表单交互后的表单。由于缺乏支持,MDN 不建议使用此方法。但是,您可以针对 Firefox 进行修改。:user-invalid
< 有一个 4 级规范/a> 即将推出的规范将提供类似的行为。Mozilla takes care of this with its own
:-moz-ui-invalid
pseudoclass that only applies to forms after they've been interacted with. MDN does not recommend using this due to a lack of support. However, you can modify it for Firefox.There's a level 4 spec for a
:user-invalid
spec on the horizon that will offer similar behavior.我创建了一个小垫片来在我的代码库中处理这个问题。我刚开始使用具有
novalidate
属性和data-validate-on="blur"
的I created a small shim to deal with this in my codebase. I just start off with my
<form/>
element having thenovalidate
property along with adata-validate-on="blur"
attribute. This watches for the first event of that type. This way you can still use the native:invalid
css selectors for the form styling.对于未通过 checkValidity 的每个元素,在发生
submit
事件之前,会在表单元素上触发一个 html5invalid
事件。您可以使用此事件将类应用于周围的表单,并仅在此事件发生后显示 :invalid 样式。然后,您的 CSS 将如下所示:
第一行删除默认样式,因此表单元素在页面加载时看起来是中性的。一旦无效事件触发(当用户尝试提交表单时),元素就会明显地呈现为无效。
There is a html5
invalid
event that fires on form elements before thesubmit
event occurs for each element that does not pass checkValidity. You can use this event to apply a class for example to the surrounding form and display :invalid styles only after this event occurs.Your CSS would then look something like this:
The first line removes the default styling, so form elements look neutral at page load. As soon as the invalid event fires (when a user tries to submit the form), the elements are visibly rendered invalid.
您可以将其设置为只有具有特定类和的元素才是必需的,是粉红色的。向每个必需的元素添加一个事件处理程序,以便在您离开该元素时添加该类。
当然
,它不会像 IE8 或更低版本中那样工作,因为 (a)
DOMContentLoaded
相对较新,在 IE8 出现时不是标准的,(b) IE8 使用 < code>attachEvent 而不是 DOM 标准的addEventListener
,并且 (c) IE8 无论如何都不会关心:required
,因为它不会技术支持HTML 5。You could make it so that only elements that have a certain class on them and are required, are pink. Add an event handler to each required element that adds that class when you leave the element.
Something like:
And of course, it won't work as is in IE8 or below, as (a)
DOMContentLoaded
is relatively new and wasn't standard when IE8 came out, (b) IE8 usesattachEvent
rather than the DOM-standardaddEventListener
, and (c) IE8 isn't going to care about:required
anyway, as it doesn't technically support HTML 5.在使用 HTML5 表单验证时,尝试使用浏览器来检测无效的提交/字段,而不是重新发明轮子。
侦听
invalid
事件以将“invalid”类添加到您的表单中。添加“invalid”类后,您可以使用 CSS3:pseudo
选择器来设计表单样式。例如:
现在,只需根据我们附加的“无效”类设置您认为合适的表单样式即可。
例如:
While using HTML5 form validation, try to use the browser to detect for invalid submissions/fields, rather than re-inventing the wheel.
Listen for the
invalid
event to add a class of 'invalid' to your form. With the 'invalid' class added, you can go to town with styling your form using CSS3:pseudo
selectors.For example:
Now, simply style your form as you see fit based on the 'invalid' class we've attached.
For example:
一个好方法是使用 CSS 类抽象
:invalid, :valid
,然后使用一些 JavaScript 来检查输入字段是否获得焦点。CSS:
JS:
演示
A good way is to abstract
:invalid, :valid
with a CSS classes and then some JavaScript to check if the input field was focused or not.CSS:
JS:
DEMO
继 agouseh 的 idea,您可以使用一些 JavaScript 来告诉提交按钮何时获得焦点,并在那时显示验证。
当提交按钮获得焦点或单击时,JavaScript 会向表单字段添加一个类(例如
submit-focussed
),然后允许 CSS 设置无效输入的样式。这遵循在用户填写完字段后显示验证反馈的最佳实践,因为根据研究,在此过程中显示它没有额外的好处。
jQuery 替代方案
Following on from agouseh's idea, you can have a bit of javascript to tell when the submit button has been focussed, and have validation show up at that time.
The javascript will add a class (eg.
submit-focussed
) to the form field when the submit button is focussed or clicked, which then allows the CSS to style invalid inputs.This follows the best practice of showing validation feedback after the user has finished filling in the fields, as according to research there is no additional benefit to showing it during the process.
jQuery alternative
从 CSS 选择器级别 4 开始,您现在可以使用
:user-invalid
选择器,从而大大简化了这一过程。https://www.w3.org/TR/selectors-4/#用户伪
Since CSS Selectors Level 4 you can now use the
:user-invalid
selector simplifying this greatly.https://www.w3.org/TR/selectors-4/#user-pseudos
这是我的方法,以避免任何未聚焦的输入的默认样式无效,您只需添加一个简单的js命令
onFocus
即可让网页识别focused
和unfocused
输入,因此所有输入都不会首先以无效的样式出现。下面你自己尝试一下:
Here is my method to avoid the default styling of any unfocused input as invalid, you just have to add a simple js command
onFocus
to let the webpage to identifyfocused
andunfocused
inputs, so all the input will not appear in the style of invalid at first place.Try it yourself below:
我无法发表评论,但要同意 @Carl 关于使用 :not(:placeholder-shown) 的非常有用的答案。正如另一条评论提到的,如果您没有占位符(正如某些表单设计所要求的那样),这仍然会显示无效状态。
要解决这个问题,只需添加一个空占位符,如下所示
然后你的 CSS,如
Worked for me!
I can't comment, but to go with @Carl's very useful answer regarding using :not(:placeholder-shown). As another comment mentioned, this will still show the invalid state if you have NO placeholder (as some form designs call for).
To solve this, simply add an empty placeholder like so
Then your CSS, something like
Worked for me!