How to use search input type in HTML?

Internet provides us a lot of services which we can access through the respective websites. Many of times, we are need to search information on a particular topic. To perform search on Internet, we can make of search engine like Google, Yahoo, Ask, Bing etc. You must have observed the textbox available for entering the search keywords. We type the keywords and it takes us to the next page where the matched results, fetched from the server, are displayed. When we design a form in HTML to capture data from the user, we use several controls. We can also make a textbox in an HTML form which can behave like a search field and we can perform Google search.

To make such a control, we use <input type=”search”> tag in HTML within the form. It looks like a normal textbox, but it is capable in performing site search.

Let us see a basic program of SEARCH control.


<html> <head><title></title> <script> function show(){ s=searchForm.searchField.value; document.write("You have searched for "+s); } </script> </head> <body> <form id="searchForm"> <label for="sea">Search for : <label> <input type="search" name="searchField"> <br><br> <input type="submit" value="Search" onclick="show()"> </form> </body> </html>

This program will display the text which is searched for on the next page using JavaScript code.

You must have observed sometimes while using Google that by default some text is coming in the search box like this −


To do this, we can use PLACEHOLDER attribute in <input> tag. Let’s see an example.

<html> <body> <form id="searchForm"> <label for="sea">Search for : </label> <input type="search" name="searchField" placeholder="Type URL"> <br><br> <input type="submit" value="Search" onclick="show()"> </form> </body> </html>


To set the default value in the search box, you can use VALUE attribute in <input> tag.

This is a very interesting attribute which can be used in <input> tag and that is SPELLCHECK attribute. This attribute can be turned ON or OFF to enable or disable the spellcheck feature. If it is ON, it will show a red wavy line under the misspelt word but if it OFF, it will simply ignore the spelling errors. By default, spellcheck attribute is set to false.

So, let us see how we can use this attribute.


<html> <body> <form id="searchForm"> <label for="sea">Search for : </label> <input type="search" name="searchField"><br><br> <input type="submit" value="Search" onclick="show()"> </form> </body> </html>


<html> <body> <form id="searchForm"> <label for="sea">Search for : </label> <input type="search" name="searchField" spellcheck=false><br><br> <input type="submit" value="Search"> </form> </body> </html>

In both the cases, the spellcheck attribute is OFF i.e. false. In this situation, it will ignore errors.

Suppose, we entered “Heaith” in the search box whereas the correct spelling is “Health”. But it is not showing any wavy line under the word.

Now let us make it true and see the results.


<html> <body> <form id="searchForm"> <label for="sea">Search for : </label> <input type="search" name="searchField" spellcheck=true><br><br> <input type="submit" value="Search"> </form> </body> </html>

We can also restrict the minimum and maximum number of characters in a search field with the help of MINLENGTH and MAXLENGTH attributes. Suppose in an example, where a user has to type/search admission number for a particular institution, it can accept minimum of 4 characters and maximum of 6 characters. But if you will not put a restriction, a user can type data of any length.


<html> <body> <form id="searchForm"> <label for="reg">Type Admission Number : </label> <input type="search" name="adm" minlength="4" maxlength="6"><br><br> <input type="submit" value="Search"> </form> </body> </html>

It is showing an error on the page because the user has not entered the minimum length of data. And if you try to exceed its maximum limit, it won’t allow to type.

We can also control the width of the textbox by using SIZE attribute. And also we can make it a mandatory field by using REQUIRED attribute.


<html> <body> <form id="searchForm"> <label for="reg">Type Admission Number : </label> <input type="search" name="adm" minlength="4" maxlength="6" size="6" required><br><br> <input type="submit" value="Search"> </form> </body> </html>

Observe the size of the box is now reduced and if you leave it blank, it is showing an error.

Updated on: 23-Aug-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started