大网数据广告活动
Web 标准与前端开发

ValidityState: typeMismatch property

· 52CE 我爱测

The read-only **`typeMismatch`** property of the `ValidityState` interface 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 `type` attribute.

HTML DOM

The read-only typeMismatch property of the ValidityState interface 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 type attribute.

If the type attribute expects specific strings, such as the input/email and input/url types and the value doesn't conform to the constraints set by the type, the typeMismatch property will be true.

The input/email input type expects one or more valid email addresses, depending on whether the multiple attribute is present. A valid email address includes an email prefix and a domain, with or without a top-level domain. If the value of the email input is not an empty string, a single valid email address, or one or more comma separated email address if the multiple attribute is present, there is a typeMismatch.

The input/url input type expects one or more valid URLs, depending on whether the multiple attribute is present. A valid URL includes a protocol, optionally with an IP address, or an optional subdomain, domain, and top-level domain combination. If the value of the URL input is not an empty string, a single valid URL, or one or more comma separated URLS if the multiple attribute is present, there is a typeMismatch.

Input type Value Expected value
input/email x@y or x@y.z email address, with or without TLD
input/url x: or x://y.z protocol or full URL with protocol

Value

A boolean that is true if the ValidityState does not conform to the constraints.

Examples

Type mismatch on input element

The typeMismatch occurs when there is a disconnect between the value expected via the type attribute and the data that is actually present. The typeMismatch is only one of the many possible errors and is only relevant for the input/email and input/url types. When the value provided doesn't match the expected value based on the type for other input types, you get different errors. For example, if the input/number input value is not a floating point number, the badInput is true. If the email is required but is empty, the ValidityState.valueMissing will be true.

<pre id="log">Validation logged here...</pre>
<p>
  <label>
    Enter an email address:
    <input id="emailInput" type="email" value="example.com" required />
  </label>
</p>
input:invalid {
  border: red solid 3px;
}
body {
  margin: 0.5rem;
}
pre {
  padding: 1rem;
  height: 2rem;
  background-color: lightgrey;
  outline: 1px solid grey;
}
const emailInput = document.getElementById("emailInput");
const logElement = document.getElementById("log");

function log(text) {
  logElement.innerText = text;
}

emailInput.addEventListener("input", () => {
  emailInput.reportValidity();
  if (emailInput.validity.valid) {
    log("Input OK…");
  } else if (emailInput.validity.typeMismatch) {
    log("Input is not an email.");
  } else {
    log(`Validation failed: ${emailInput.validationMessage}`);
  }
});

Examples

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。