Javascript DOM HTML Input Email required Property get

Introduction

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

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  E-mail: <input type="email" id="myEmail" name="usremail" required>
  <input type="submit">
</form>/*from  w w w .j a va 2 s . co  m*/
<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 required property sets or gets whether an email field must be filled out before submitting a form.

This property mirrors the HTML required attribute.

The required property accepts and returns a boolean value.

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

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




PreviousNext

Related