The required
property sets or gets
whether a password field must be filled before submitting a form.
required |
Yes | 10.0 | Yes | Yes | Yes |
Return the required property.
var v = passwordObject.required
Set the required property.
passwordObject.required=true|false
Value | Description |
---|---|
true|false | Set whether a password field should be a required field in the form.
|
A Boolean type value, true if the password field is a required part of form submission, otherwise false.
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:
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: