Javascript DOM HTML Input URL required Property get

Introduction

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

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

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

elements with type="url" are not supported in Safari.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Homepage: <input type="url" id="myURL" name="website" required>
  <input type="submit">
</form>/*w w  w.j  a  va2  s  . co m*/
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

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

</body>
</html>

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

This property mirrors the HTML required attribute.

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

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




PreviousNext

Related