Javascript Reference - HTML DOM Input Password required Property








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

Browser Support

required Yes 10.0 Yes Yes Yes

Syntax

Return the required property.

var v = passwordObject.required 

Set the required property.

passwordObject.required=true|false

Property Values

Value Description
true|false Set whether a password field should be a required field in the form.
  • true - The password field is a required field
  • false - Default. The password field is not a required field




Return Value

A Boolean type value, true if the password field is a required part of form submission, otherwise false.

Example

The following code shows how to set a password field to be a required field.


<!DOCTYPE html>
<html>
<body>
<!--  www .  ja  va2 s. c om-->
<form action="url">
   Password: <input type="password" id="myPsw" name="usr_psw">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myPsw").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get if a password field is a required field.


<!DOCTYPE html>
<html>
<body>
<!--from ww  w .j a  v a  2  s .  c  om-->
<form action="url">
  Password: <input type="password" id="myPsw" name="usr_psw" required>
  <input type="submit">
</form>
<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 code above is rendered as follows: