Javascript Reference - HTML DOM Input Email required Property








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

Browser Support

required Yes 10.0 Yes Yes Yes

Syntax

Return the required property.

var v = emailObject.required 

Set the required property.

emailObject.required=true|false

Property Values

Value Description
true|false Specifies whether an email field should be required for form submission.
  • true - The email field is required
  • false - Default. The email field is not required




Return Value

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

Example

The following code shows how to set an email field to be required.


<!DOCTYPE html>
<html>
<body>
<!--  ww w. j a va2s.c om-->
<form action="url">
  E-mail: <input type="email" id="myEmail" name="usremail">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myEmail").required = true;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get if an email field must be filled before submitting a form.


<!DOCTYPE html>
<html>
<body>
<!-- w  w  w  .  j a va  2 s  .  c  o m-->
<form action="url">
  E-mail: <input type="email" id="myEmail" name="usremail" required>
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myEmail").required;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: