Javascript Reference - HTML DOM Input URL required Property








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

Browser Support

required Yes 10.0 Yes Yes Yes

Syntax

Return the required property.

var v = urlObject.required 

Set the required property.

urlObject.required=true|false

Property Values

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




Return Value

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

Example

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


<!DOCTYPE html>
<html>
<body>
<!-- w ww. j  a  v a  2 s.  com-->
<form action="url">
  Homepage: <input type="url" id="myURL" name="website">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myURL").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to check if a URL field is required.


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