Select checkValidity() Method
Example
Check whether the input element has any constraints, and whether the user input satisfies them:
<input id="id1" type="number" min="100" max="300" required>
<button onclick="myFunction()">OK</button>
<p id="demo"></p>
<script>
function myFunction() {
var
inpObj = document.getElementById("id1");
if (!inpObj.checkValidity())
{
document.getElementById("demo").innerHTML =
inpObj.validationMessage;
} else {
document.getElementById("demo").innerHTML = "Input OK";
}
}
</script>
Try it Yourself »
Description
The checkValidity() method checks whether the element has any constraints and whether it satisfies them.
If the element fails its constraints, the browser fires a cancelable invalid event at the element, and then returns false.
Browser Support
Method | |||||
---|---|---|---|---|---|
checkValidity() | Yes | Yes | 4.0 | Yes | Yes |
Syntax
selectObject.checkValidity()
Technical Details
Return Value: | Returns true if an input element contains valid data, otherwise false |
---|
More Examples
Example
Add a "Kiwi" option at the beginning of a drop-down list:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[0]);
Try it Yourself »
Example
Add a "Kiwi" option at index position "2" of a drop-down list:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[2]);
Try it Yourself »
Example
Add an option before a selected option in a drop-down list:
var x = document.getElementById("mySelect");
if (x.selectedIndex >= 0) {
var option = document.createElement("option");
option.text = "Kiwi";
var sel = x.options[x.selectedIndex];
x.add(option, sel);
}
Try it Yourself »
❮ Select Object