avoiding empty submissions

I use Atomz search to allow users to search my site and the weekly report always has the same top query. It is (drum roll please):

blank query

I have no idea why someone would search for nothing, because you get just that as a result, but search for nothing they did.

I decided to put a stop to it. In the header I added a Javascript function that checks the value of the search field and if blank, pops up a message asking the user to please enter a search phrase. Here’s the code to do that.

<script type="text/javascript" language="Javascript">
function validateSearch()
    if ( document.search.sp_q.value == "" )
    if ( document.search.sp_q.value.match( /^\s*$/ ) != null )
        alert( "Please enter a search phrase" );
        return false;
    return true;

The Javascript method is executed when the user hits the search button by adding the bold text.

<form name="search" method="get" action="http://search.atomz.com/search/"
 onSubmit="return validateSearch();">
<input type=text size=30 name="sp_q"><br>
<input type=submit value="Search this site">

Blank queries still show up occasionally, but they’re probably from users who have Javascript disabled. An even better solution would be for the server to make sure the search isn’t blank too, but since I don’t have access to the server-side code, this is the best I can do.


 (Post a comment) | Comments RSS feed
  1. As a jerk, I could not avoid typing in a space into the search field and then pressing search. Sorry, my willpower is weak.

    Comment by Steve on July 20, 2004 @ 2:02 pm
  2. It is amazing (and laughable) where cleverness is required!

    Comment by Babs on July 20, 2004 @ 2:55 pm
  3. Drat. That chaps my hide.

    Please see the altered version of the function that checks for spaces as well.

    Comment by dan on July 20, 2004 @ 5:13 pm
  4. Oh do you know how much of an urge I have to do a bunch of blank searches now?

    Comment by Kristine on July 20, 2004 @ 5:14 pm
  5. Heh heh heh. Try it. They will be no match for my snazzy (slightly modified) function.

    Comment by dan on July 20, 2004 @ 5:21 pm
  6. Flaw in your plan? At your site you need to enter some text, then the results of the query are displayed on the atomz site. On the results page there is another query box, which _allows_ for a no text search. Do those show in your logs as well?

    Comment by Chad on July 22, 2004 @ 8:19 am
  7. Chad: Excellent point – maybe that’s why I keep getting blank queries. I modified the search results template to also not allow blank queries. The only problem is it no longer displays the last query in the search box, but I’m willing to live with that.

    Comment by dan on July 22, 2004 @ 9:27 am

Comments are closed