Devuelve verdadero si un elemento de entrada contiene datos válidos.
Establece la propiedad validationMessage de un elemento de entrada.
Si un campo de entrada contiene datos no válidos, muestra un mensaje:
<input id="id1" type="number" min="100" max="300"
required>
<button onclick="myFunction()">OK</button>
<p id="demo"></p>
<script>
function myFunction() {
const inpObj = document.getElementById("id1");
if (!inpObj.checkValidity()) {
document.getElementById("demo").innerHTML = inpObj.validationMessage;
}
}
</script>
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<p>Enter a number and click OK:</p>
<input id="id1" type="number" min="100" max="300" required>
<button onclick="myFunction()">OK</button>
<p>If the number is less than 100 or greater than 300, an error message will be displayed.</p>
<p id="demo"></p>
<script>
function myFunction() {
const inpObj = document.getElementById("id1");
if (!inpObj.checkValidity()) {
document.getElementById("demo").innerHTML = inpObj.validationMessage;
} else {
document.getElementById("demo").innerHTML = "Input OK";
}
}
</script>
</body>
</html>
Contiene propiedades booleanas relacionadas con la validez de un elemento de entrada.
Contiene el mensaje que mostrará un navegador cuando la validez sea falsa.
Indica si un elemento de entrada será validado.
La propiedad de validez de un elemento de entrada contiene un número de propiedades relacionadas con la validez de los datos:
Establecer en verdadero, si se establece un mensaje de validez personalizado.
Se establece en verdadero si el valor de un elemento no coincide con su atributo de patrón.
Se establece en verdadero si el valor de un elemento es mayor que su atributo máximo.
Se establece en verdadero si el valor de un elemento es menor que su atributo mínimo.
Se establece en verdadero si el valor de un elemento no es válido según su atributo de paso.
Se establece en verdadero si el valor de un elemento excede su atributo maxLength.
Se establece en verdadero si el valor de un elemento no es válido según su atributo de tipo.
Se establece en verdadero si un elemento (con un atributo obligatorio) no tiene valor.
Se establece en verdadero, si el valor de un elemento es válido.
Si el número en un campo de entrada es mayor que 100 (el max
de la entrada atributo), muestra un mensaje:
<input id="id1" type="number" max="100">
<button onclick="myFunction()">OK</button>
<p id="demo"></p>
<script>
function myFunction() {
let text = "Value OK";
if (document.getElementById("id1").validity.rangeOverflow) {
text = "Value too large";
}
}
</script>
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<p>Enter a number and click OK:</p>
<input id="id1" type="number" max="100">
<button onclick="myFunction()">OK</button>
<p>If the number is greater than 100 (the input's max attribute), an error message will be displayed.</p>
<p id="demo"></p>
<script>
function myFunction() {
let text;
if (document.getElementById("id1").validity.rangeOverflow) {
text = "Value too large";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
Si el número en un campo de entrada es menor que 100 (el atributo min
de la entrada), muestre un mensaje:
<input id="id1" type="number" min="100">
<button onclick="myFunction()">OK</button>
<p id="demo"></p>
<script>
function myFunction() {
let text = "Value OK";
if (document.getElementById("id1").validity.rangeUnderflow) {
text = "Value too small";
}
}
</script>
Pruébelo usted mismo →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<p>Enter a number and click OK:</p>
<input id="id1" type="number" min="100">
<button onclick="myFunction()">OK</button>
<p>If the number is less than 100 (the input's min attribute), an error message will be displayed.</p>
<p id="demo"></p>
<script>
function myFunction() {
let text;
if (document.getElementById("id1").validity.rangeUnderflow) {
text = "Value too small";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>