Prevent a search box from being submitted without entering any text

Test blog

Posted by Shengwen on August 23, 2016

Simple code to prevent a search box from being submitted without entering any text. Listen to the submit event of the form and stop the event if there is no text in the search box. Create a js file:

1
2
3
4
5
6
7
$(function(){
    $('#searchForm').submit(function(){
        if($("#textbox").val().trim())
            return true;
        return false;
    });
});

Include this js file and jQurey library in the view.