Evento oninvalid
Definición y uso
Se produce el evento oninvalid cuando un elemento <input> que se puede enviar es inválido.
Por ejemplo, si se ha configurado la propiedad required y el campo está vacío, el campo de entrada no es válido (la propiedad required especifica que el campo de entrada debe estar llenado antes de enviar el formulario).
Instance
Example 1
If the input field is invalid, display some text:
<input type="text" oninvalid="alert('You must fill out the form!');" required>
More TIY examples are at the bottom of the page.
Syntax
In HTML:
<element oninvalid="myScript">
In JavaScript:
object.oninvalid = function(){myScript};
In JavaScript, use the addEventListener() method:
object.addEventListener("invalid", myScript);
Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.
Technical details
Bubbling: | Not supported |
---|---|
Cancellable: | Support |
Event type: | Event |
Supported HTML tags: | <input> |
DOM version: | Level 3 Events |
Browser support
The numbers in the table indicate the first browser version that fully supports the event.
Event | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
oninvalid | Support | 10.0 | Support | Support | Support |
More examples
Example 2
If the input field contains fewer than 6 characters, display some text:
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>
Example 3
If the input field contains a number less than 2 or greater than 5, display some text:
Number: <input type="number" id="myInput" name="quantity" min="2" max="5" required> <script> document.getElementById("myInput").addEventListener("invalid", myFunction); function myFunction() { alert("Debe seleccionar un número entre 2 y 5. Usted eligió: " + this.value); } </script>