Javascript DOM HTML Input Password required Property get

Introduction

Find out if a password field must be filled out before submitting a form:

var x = document.getElementById("myPassword").required;

Click the button to find out if the password field must be filled out before submitting the form.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Password: <input type="password" id="myPsw" name="usr_psw" required>
  <input type="submit">
</form>// www.j  a  va2  s  .c o m

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myPsw").required;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The required property sets or gets whether a password field must be filled out before submitting a form.

This property mirrors the HTML required attribute.

Value Description
true The password field is a required part of form submission
false Default. The password field is not a required part of form submission

The required property returns true if the password field is a required part of form submission, otherwise it returns false.




PreviousNext

Related