小码哥的IT人生

oninvalid 事件

JavaScript基础 2022-06-08 12:00:36小码哥的IT人生shichen

oninvalid 事件

实例

如果输入字段无效,则提示一些文本:

<input type="text" oninvalid="alert('You must fill out the form!');" required>

完整实例:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/html/action_page.php" method="get">
  Name: <input type="text" oninvalid="alert('You must fill out the form!');" name="fname" required>
  <input type="submit" value="Submit">
</form>
<p>如果您点击提交,而不填写文本字段,将出现警告消息。</p>
<p><b>注释:</b>Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

页面下方有更多 TIY 实例。

定义和用法

当可提交的 <input> 元素无效时,会发生 oninvalid 事件。

例如,如果设置了 required 属性,并且字段为空,则输入字段无效(required 属性指定输入字段必须在提交表单之前填写)。

浏览器支持

表中的数字注明了完全支持该事件的首个浏览器版本。

事件 Chrome IE Firefox Safari Opera
oninvalid 支持 10.0 支持 支持 支持

语法

在 HTML 中:

<element oninvalid="myScript">

完整实例:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/html/action_page.php" method="get">
  Name: <input type="text" oninvalid="alert('You must fill out the form!');" name="fname" required>
  <input type="submit" value="Submit">
</form>
<p>如果您点击提交,而不填写文本字段,将出现警告消息。</p>
<p><b>注释:</b>Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

在 JavaScript 中:

object.oninvalid = function(){myScript};

完整实例:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/html/action_page.php" method="get">
  Name: <input type="text" id="myInput" name="fname" required>
  <input type="submit" value="Submit">
</form>
<p><b>注释:</b>Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
<script>
document.getElementById("myInput").oninvalid = function() {myFunction()};
function myFunction() {
  alert("You must fill out the form!");
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

在 JavaScript 中,使用 addEventListener() 方法:

object.addEventListener("invalid", myScript);

完整实例:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/html/action_page.php" method="get">
  Name: <input type="text" id="myInput" name="fname" required>
  <input type="submit" value="Submit">
</form>
<p>如果您点击提交,而不填写文本字段,将出现警告消息。</p>
<p><b>注释:</b>Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
  alert("You must fill out the form!");
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

注释:Internet Explorer 8 或更早的版本不支持 addEventListener() 方法

技术细节

冒泡: 不支持
可取消: 支持
事件类型: Event
支持的 HTML 标签: <input>
DOM 版本: Level 3 Events

更多实例

示例代码:

如果输入字段包含少于 6 个字符,则提示一些文本:

Name: <input type="text" id="myInput" name="fname" pattern=".{6,}" required>
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
  alert("Must contain 6 or more characters");
}
</script>

完整实例:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/html/action_page.php" method="get">
  Name: <input type="text" id="myInput" name="fname" pattern=".{6,}" required>
  <input type="submit" value="Submit">
</form>
<p>If you submit the form with less than 6 characters, an alert message will occur.</p>
<p><b>注释:</b>Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
  alert("Must contain 6 or more characters");
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

示例代码:

如果输入字段包含小于 2 或大于 5 的数字,则提示一些文本:

Number: <input type="number" id="myInput" name="quantity" min="2" max="5" required>
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
  alert("You must pick a number between 2 and 5. You chose: " + this.value);
}
</script>

完整实例:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/html/action_page.php" method="get">
  Pick a number between 2 and 5: <input type="number" id="myInput" name="quantity" min="2" max="5" required>
  <input type="submit" value="Submit">
</form>
<p>如果您提交表单的数字小于 2 或大于 5,将出现警告消息。</p>
<p><b>注释:</b>Internet Explorer 9 及更早版本或 Safari 不支持 oninvalid 事件。</p>
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
  alert("You must pick a number between 2 and 5. You chose: " + this.value);
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024