JS Coding Questions Logo
JS Coding Questions
#275💼 Interview💻 Code

What are the DOM methods available for constraint validation

Advertisement

728x90

The below DOM methods are available for constraint validation on an invalid input,

1. checkValidity(): It returns true if an input element contains valid data.

2. setCustomValidity(): It is used to set the validationMessage property of an input element.

Let's take an user login form with DOM validations

javascript
1function myFunction() {
2  var userName = document.getElementById("uname");
3  if (!userName.checkValidity()) {
4  document.getElementById("message").innerHTML =
5  userName.validationMessage;
6  } else {
7  document.getElementById("message").innerHTML =
8  "Entered a valid username";
9  }
10  }

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 18

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
275of476
What are the DOM methods available for constraint validation | JSCodingQuestions.com