HTML oninvalid Attribute

HTML oninvalid Attribute: This attribute is defined as an oninvalid error occurs when a submittable <input> element is invalid. The script runs when a user clicks on the submit button. The required input field must be filled before submitting it. It was newly added to HTML5.

HTML oninvalid Attribute

This attribute can be applied to all the HTML elements. And this attribute contains a single value script which works when oninvalid attribute called. It is supported by all HTML elements. HTML4 does not support the oninvalid attribute.

Syntax: <element oninvalid = “script”>



Browser Support

This attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Safari- does not support
  • Opera
  • Internet Explorer-10.0

Example: for <input> element

<!DOCTYPE html>
<html>
<body>
<form action="/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>An alert message will occur if you click submit without filling out the text field.</p>
</body>
</html>

Output:

HTML oninvalid attribute