csshtml表单样式_再见,jQuery验证:CSSHTML5表单错误

csshtml表单样式

CSS allows for the detection of the status of HTML5 form elements through the use of the pseudo-class selectors :valid and :invalid. For the purposes of demonstration I’ll use the email input to start, as it has built-in validation:

CSS允许通过使用伪类选择器 :valid:invalid来检测HTML5表单元素的状态。 出于演示目的,我将使用电子邮件输入来启动,因为它具有内置的验证:

<input type="email" name="email" id="email">

Detecting whether the user has entered information correctly in the field is simple, using an attribute selector combined with the :valid pseudo-class:

使用attribute selector:valid伪类结合使用,可以很容易地检测用户是否在字段中正确输入了信息:

input[type=email]:valid {
	/* appearance for valid entry */
}

Changing the appearance of the input is good and fine, but I wanted to take the effect further, and add an error message if the information entered by the user was incorrect. One would think we could chain pseudo-class selectors together:

更改input的外观很好,但是我想进一步提高效果,如果用户输入的信息不正确,则添加错误消息。 有人认为我们可以将伪类选择器链接在一起:

input[type=email]:invalid:after {
	content: “Error message”;
}

Sadly, we cannot use :after or :before directly on a form input. Like the <img> tag it is a replaced element. All is not yet lost: there is another way.

可悲的是,我们不能在表单input上直接使用:after:before 。 像<img>标签一样,它是一个替换元素 。 一切还没有丢失:还有另一种方式。

The technique that follows can be used on any input: let’s change the type attribute to text to keep things interesting. For this example, let’s say we are looking for a user’s first name. In that case, the regular expression we’ll use for pattern will be very simple: we won’t accept numerals, but anything else comprised of at least two upper or lowercase letters will be fine:

以下技术可用于任何输入:让我们将type属性更改为text以使事情有趣。 对于此示例,假设我们正在寻找用户的名字。 在这种情况下,我们将用于pattern正则表达式将非常简单:我们将不接受数字,但由至少两个大写或小写字母组成的其他任何形式都可以:

<input type="text" name="firstname" id="firstname"  pattern="[^0-9][A-Za-z]{2,20}">

I’ll also add a span immediately after the input, with a title attribute that contains an error message associated with invalid content:

我还将在输入之后立即添加一个span ,其title属性包含与无效内容相关的错误消息:

<input type="text" name="firstname" id="firstname" pattern="[^0-9][A-Za-z]{2,20}">
<span title="Must be at least two letters, no numbers"></span>

We want to complete the span with the text of the title attribute. This will also mean that browsers that don’t support this part of CSS won’t see the text, creating a state of graceful degradation.  We’ll make sure it’s the span element immediately after the input by using a sibling selector:

我们要使用title属性的文本来完成span 。 这也意味着不支持CSS此部分的浏览器将看不到文本,从而造成正常降级的状态。 我们将使用同级选择器来确保它是输入之后的span元素:

input ~ span:after {
	content: attr(title);
	color: red;
	margin-left: 0.6rem;
}

The default appearance of any generated content in the span will be invisible, via use of opacity:

通过使用opacityspan内任何生成的内容的默认外观将是不可见的:

input ~ span:after {
	content: attr(title);
	color: red;
	margin-left: 0.6rem;
	opacity: 0;
}

… but we’ll change that based on the invalidity of the information entered into the field immediately before it:

…但是我们将根据紧接在该字段之前的字段中输入的信息的无效性来进行更改:

input:invalid ~ span:after {
	opacity: 1;
}

Done! But the appearance of the error message is a little sudden and clunky: it shows up just as soon as we type the first letter in the field, potentially distracting and confusing users. We’ll delay and fade in the message by using transition-property, duration and delay:

做完了! 但是错误消息的出现有点突然且笨拙:只要我们在该字段中输入第一个字母,它就会立即出现,这可能会分散用户的注意力并使他们感到困惑。 我们将使用transition-propertydurationdelay来延迟和淡入消息:

input ~ span:after {
	content: attr(title);
	color: red;
	margin-left: 0.6rem;
	opacity: 0;
	transition: opacity 2s 2s;
}

You can see the completed effect in the small form at the top of this article.

您可以在本文顶部的小格式中看到完成的效果。

These techniques do not completely eliminate JavaScript from forms: you’ll still need the scripting technology for features like AJAX validation (checking if a username is already registered in a database, for example), or to recreate the same effect in older browsers. JavaScript can also be used to customize the browser's own built-in validation error messages, as I show in the next article.

这些技术不能完全从表单中消除JavaScript :您仍然需要脚本技术来实现诸如AJAX验证之类的功能(例如,检查用户名是否已在数据库中注册),或者在较旧的浏览器中重新创建相同的效果。 JavaScript也可以用于自定义浏览器的内置验证错误消息 ,如我在下一篇文章中所示。

You’ll also need or some other server-side technology to act as a secure, impassable fallback for ultimate validation of user-submitted data. But the techniques demonstrated here continue to do what CSS should: push JavaScript into more advanced and useful areas, rather than being used for simple actions on a page.

您还需要或其他服务器端技术,以作为对用户提交的数据进行最终验证的安全,不可逾越的后备。 但是,这里展示的技术仍将继续执行CSS应做的工作:将JavaScript推入更高级和有用的区域,而不是用于页面上的简单操作。

更多资源 (Further Resources)

HTML5 Doctor, in the form of Peter Gasston, have taken the same idea expressed here and run with it in interesting new directions for other HTML5 form inputs.

以Peter Gasston的形式出现HTML5 Doctor已经采用了此处表达的相同思想,并以有趣的新方向将其运用于其他HTML5表单输入

翻译自: https://thenewcode.com/462/Goodbye-JQuery-Validation-HTML5-Form-Errors-With-CSS

csshtml表单样式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值