Javascript Reference - HTML DOM Input Search required Property








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

Browser Support

Input Search required Yes 10.0 Yes Yes Yes

Syntax

Return the required property.

var v = searchObject.required 

Set the required property.

searchObject.required=true|false

Property Values

Value Description
true|false Set whether a search field is a required part of form.
  • true - The search field is required
  • false - Default. The search field is not required




Return Value

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

Example

The following code shows how to set a search field to be a required part of form submission.


<!DOCTYPE html>
<html>
<body>
<!--   ww  w  .ja v a  2  s.c  o m-->
<form action="url">
  Search: <input type="search" id="mySearch" name="filter">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("mySearch").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
<!--from w  w w .j  a  v a  2  s. c o  m-->
<form action="url">
  Search: <input type="search" id="mySearch" name="filter" required>
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows: