HTML novalidate 属性

htmlweb developmentfront end technology更新于 2025/12/21 6:36:17

HTML novalidate 属性定义在提交表单时,不应在 HTML 文档中验证表单数据。

语法

以下是语法 −

<form novalidate></form>

让我们看一个 HTML novalidate 属性的示例 −

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
   }
   input[type='text'] {
      width: 300px;
      padding: 8px 16px;
      border: 2px solid #fff;
      background: transparent;
      border-radius: 20px;
      display: block;
      margin: 1rem auto;
      outline: none;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 20px;
      width: 330px;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
   ::placeholder {
      color: #000;
   }
</style>
<body>
<h1>HTML novalidate attribute Demo</h1>
<form>
<input type="text" placeholder="Enter your name" required>
<input type="submit" value="Submit" class="btn" onclick='check()'>
</form>
<button type='button' class="btn" onclick="set()">Set No Validation</button>
<div class="show"></div>
<script>
   function set() {
      document.querySelector('form').setAttribute('novalidate', 'true');
   }
</script>
</body>
</html>

输出

尝试点击"提交"按钮,但不在文本字段中输入任何姓名,它将显示一条警告消息"减号"

现在点击"设置无验证"按钮,在表单元素上设置 novalidate 属性,然后尝试提交表单,但不在文本字段中输入任何姓名,这次它将轻松提交,而不会显示任何警告消息 


相关文章


有用资源