oninvalid Event
Example
Alert some text if an input field is invalid:
More "Try it Yourself" examples below.
Definition and Usage
The oninvalid event occurs when a submittable <input> element is invalid.
For example, the input field is invalid if the required attribute is set and the field is empty (the required attribute specifies that the input field must be filled out before submitting the form).
Browser Support
The numbers in the table specify the first browser version that fully supports the event.
Event | |||||
---|---|---|---|---|---|
oninvalid | Yes | 10.0 | Yes | Not supported | Yes |
Syntax
In JavaScript, using the addEventListener() method:
Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.
Technical Details
Bubbles: | No |
---|---|
Cancelable: | Yes |
Event type: | Event |
Supported HTML tags: | <input> |
DOM Version: | Level 3 Events |
More Examples
Example
Alert some text if an input field contains less than 6 characters:
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
alert("Must contain 6 or more characters");
}
</script>
Example
Alert some text if an input field contains a number that is less than 2 or greater than 5:
<script>
document.getElementById("myInput").addEventListener("invalid", myFunction);
function myFunction() {
alert("You must pick a number between 2 and 5. You chose: " + this.value);
}
</script>
Related Pages
JavaScript Tutorial: JavaScript Forms
Event Object