oninvalid

The oninvalid event is triggered when a submittable <input> element fails validation.

oninvalid event

The oninvalid event occurs when a <input> element fails validation, allowing for custom validation messages or specific actions to be handled.

Syntax

index.html
<element oninvalid="script">

Example

::event-oninvalid ::
index.html
<!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>If you click submit, without filling out the text field, an alert message will occur.</p>
<p><strong>Note:</strong> The oninvalid event is not supported in Safari.</p>
</body>
</html>

Values

Conclusion

The oninvalid event is essential for handling form validation errors. It enables developers to provide custom feedback or actions when a <input> element fails validation, improving the user experience and ensuring the correct data is submitted.