promaxdev
promaxdev

Reputation: 495

How can I get the HTML5 validity state of an input text box?

How can I retrieve the HTML 5 'validity' status of a text box?

For instance, I have a text input with type="email". When the user enters the wrong value, the text box shows a red border (in Firefox browser).

How can I check the 'validity-state' of the input box?

Upvotes: 17

Views: 13117

Answers (2)

Benny Ng
Benny Ng

Reputation: 328

There is an event oninvalid for invalid input, you can register the event if you want to act on it.

Upvotes: 3

Ram
Ram

Reputation: 144739

You can use validity property:

var isValid = document.getElementById('email').validity.valid;

Or checkValidity() method:

var isValid = document.getElementById('email').checkValidity();

Demo | Reference.

Upvotes: 21

Related Questions