admin 管理员组

文章数量: 1086019

I'm using the Topcoat CSS library. You can see the code snippet I'm having problems with here.

In the style sheet, there is the following to put a red border around an input when invalid:

.topcoat-text-input--large:invalid {
  border: 1px solid #EC514E;
}

My HTML content is:

<input type="text" class="topcoat-text-input--large" id="email" placeholder="email" value="<%= model.email %>">

How do I set the input to use the CSS for invalid? If I change the class of the input from:

topcoat-text-input--large

to

topcoat-text-input--large:invalid

I don't get the red border. How can I use this CSS?

I'm using the Topcoat CSS library. You can see the code snippet I'm having problems with here.

In the style sheet, there is the following to put a red border around an input when invalid:

.topcoat-text-input--large:invalid {
  border: 1px solid #EC514E;
}

My HTML content is:

<input type="text" class="topcoat-text-input--large" id="email" placeholder="email" value="<%= model.email %>">

How do I set the input to use the CSS for invalid? If I change the class of the input from:

topcoat-text-input--large

to

topcoat-text-input--large:invalid

I don't get the red border. How can I use this CSS?

Share Improve this question edited Jun 19, 2022 at 11:54 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Mar 24, 2014 at 21:21 user1716672user1716672 1,0732 gold badges22 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

The :invalid pseudo-class is triggered when the pattern input attribute is not matched.

In the linked example:

<input type="text" class="topcoat-text-input--large" placeholder="text" value="fail" pattern="not-fail">

If you type "not-fail" into the box, it will turn blue. If you type anything else, it does not match the pattern and is invalid.

For more information, see the MDN article on pattern.

A regular expression that the control's value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is text, search, tel, url or email; otherwise it is ignored. The regular expression language is the same as JavaScript's. The pattern is not surrounded by forward slashes.

The :invalid part of the selector is not part of the class, but something called a pseudo class. In the case of :invalid, it only activates when (from MDN):

an <input> or <form> element whose content fails to validate according to the input's type setting.

本文标签: javascriptCSS class with colonStack Overflow