ValidityState.patternMismatch - Web APIs 编辑

Draft

This page is not complete.

The read-only patternMismatch property of a ValidityState object indicates if the value of an <input>, after having been edited by the user, does not conform to the constraints set by the element's pattern attribute.

If the field supports the pattern attribute -- which means the <input> is of type text, tel, email, url, password,  or search -- and the pattern value is set to a valid regular expression, if the value don't doesn't conform to the constraints set by the pattern value, the patternMismatch property will be true.

Examples

Given the following:

<p>
 <label>Enter your phone number in the format (123)456-7890
  (<input name="tel1" type="tel" pattern="[0-9]{3}" placeholder="###" aria-label="3-digit area code" size="2"/>)-
   <input name="tel2" type="tel" pattern="[0-9]{3}" placeholder="###" aria-label="3-digit prefix" size="2"/> -
   <input name="tel3" type="tel" pattern="[0-9]{4}" placeholder="####" aria-label="4-digit number" size="3"/>
 </label>
</p>

Here we have 3 sections for a north American phone number with an implicit label encompassing all three components of the phone number, expecting 3-digits, 3-digits and 4-digits respectively, as defined by the pattern attribute set on each.

If the values are too long or too short, or contain characters that aren't digits, patternMismatch will be true. When true, the element matches the :invalid CSS pseudo-classes.

input:invalid {
  border: red solid 3px;
}

Note, in this case, we get a patternMismatch not a validityState.tooLong or validityState.tooShort if the values are too long or too short because it is the pattern that is dictating the length of the value. Had we used minlength and maxlength attributes instead, we may have seen validityState.tooLong or validityState.tooShort being true.

Note: The email input type requires, at minimum, a match of x@y and the url type requires, at minimum, a match to x:, with no pattern attribute present. When invalid, the validityState.typeMismatch will be true, if there is no pattern attribute (or if the pattern attribute is not valid for that input type).

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'ValidityState.patternMismatch' in that specification.
Living Standard
HTML 5.1
The definition of 'ValidityState.patternMismatch' in that specification.
Recommendation
HTML5
The definition of 'ValidityState.patternMismatch' in that specification.
Recommendation

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:67 次

字数:6192

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文