JavaScript Validation API
Constraint validation DOM methods
| Property | Description |
|---|---|
| checkValidity() | Returns true if the data in the input element is valid, otherwise returns false. |
| setCustomValidity() |
Sets the validationMessage property of the input element, a method for customizing error messages. After setting a custom message with setCustomValidity, validity.customError becomes true, and checkValidity always returns false. To re-evaluate, you need to cancel the custom message, as follows: |
setCustomValidity('') setCustomValidity(null) setCustomValidity(undefined) |
The following example returns an error message if the input is invalid:
The checkValidity() method
<input id="id1" type="number" min="100" max="300" required>
<button onclick="myFunction()" Validation button>
<p id="demo" p>
<script function myFunction(){var inpObj=document>.
getElementById
(
"
id1
"
)
;
if
(
inpObj
.
checkValidity
(
)
==
false
)
{
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
inpObj
.
validationMessage
;
}
}
script
Try it »
Constraint validation DOM properties
| property | Description |
|---|---|
| validity | A boolean value property that returns whether the input value is valid |
| validationMessage | Browser error message |
| willValidate | Specifies whether the input is required to be validated |
Validity properties
The input element's validity propertycontains a series of data properties about validity:
| property | Description |
|---|---|
| customError | Set to true if a custom validity message has been set. |
| patternMismatch | Set to true if the element's value does not match its pattern attribute. |
| rangeOverflow | Set to true if the element's value is greater than the set maximum. |
| rangeUnderflow | Set to true if the element's value is less than the set minimum. |
| stepMismatch | Set to true if the element's value is not set according to the specified step attribute. |
| tooLong | Set to true if the element's value exceeds the length set by the maxLength attribute. |
| typeMismatch | Set to true if the element's value is not of the expected type. |
| valueMissing | Set to true if the element (required attribute) has no value. |
| valid | Set to true if the element's value is valid. |
Example
If the input value is greater than 100, display a message:
The rangeOverflow property
<input id="id1" type="number" max="100">
<button onclick="myFunction()" Validation button>
<p id="demo" p>
<script function myFunction(){var txt="">;
if
(
document
.
getElementById
(
"
id1
"
)
.
validity
.
rangeOverflow
)
{
txt
=
"
The input value is too large
"
;
}
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
txt
;
}
script
Try it »
If the input value is less than 100, display a message:
The rangeUnderflow property
<input id="id1" type="number" min="100" required>
<button onclick="myFunction()" OK button>
<p id="demo" p>
<script function myFunction(){var txt="">;
var
inpObj
=
document
.
getElementById
(
"
id1
"
)
;
if
(
!
isNumeric
(
inpObj
.
value
)
)
{
txt
=
"
What you entered is not a number
"
;
}
else
if
(
inpObj
.
validity
.
rangeUnderflow
)
{
txt
=
"
The input value is too small
"
;
}
else
{
txt
=
"
Correct input
"
;
}
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
txt
;
}
//
Determine whether the input is a number
function
isNumeric
(
n
)
{
return
!
isNaN
(
parseFloat
(
n
)
)
&&
isFinite
(
n
)
;
}
script
Try it »