Jump to content

Recommended Posts

I have a search query text field and wish to prevent anyone from searching for less that 3 characters in length.

 

I have got this code from some site but could not change it to prevent less then three characters being searched.

 

What is the best way to do this without having to have them submit and then be told, but have them told via javascript before they submit.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title></title>
    <script type="text/javascript">
    // <![CDATA[
    function Minimum(obj,min){
     if (obj.value.length<min) alert('min of '+min);
    } else {
document.form1.searchButton.disabled=false;
}
    // ]]>
    </script>
  </head>
  <body>
    <form method="post" action="" name="form_2">
      <p>
        <input type="text" name="box1" size="30" onkeypress="Minimum(this,4);" />
        <input type="submit" value="Submit" name="Button" disabled="disabled" />
      </p>
    </form>
  </body>
</html>

When you say "without having to have them submit and then be told" - I assume you mean on the next request? Not actually as they click the submit button? A really easy solution is to just display an alert() when they submit the form having entered less than 3 characters:

 

<script type="text/javascript">
window.onload = function() {
    document.search_form.onsubmit = function() {
        var query = document.search_form.query.value;
        if (query.length < 3) {
            alert('Please enter a search query more than 3 characters.');
            return false;
        }
    }
}
</script>

<form method="get" name="search_form" action="">
    Search for:
    <input type="text" name="query" />
    <input type="submit" value="Search" />
</form>

 

If you use this code, remember to modify the form and input names within the JS to match your form's mark-up.

 

Edit: Also you should add the window.onload block to within your <head>, and obviously the form in <body>. If you already have an event assigned to the onload of the window, you need to merge the two as you can't have more than one.

ah just found out that this works but it doesn't !

 

It prevents all other submit buttons from being click, the search submit button is not the only submit button on my page!

 

What do I change in this code so it is secific to just one button being click that a check is done on a secific text file that it contains three or more characters, before it is sent.

 

window.onload = function() {
    document.form1.onsubmit = function() {
        var query = document.form1.query.value;
        if (query.length < 3) {
            alert('Please enter a search query more than 3 characters.');
            return false;
        }
    }
}

The event in your code is only bound to the form named "form1". The event shouldn't be getting triggered for other forms. Going back to the original example I provided, add the the following form on to the end. You'll see that when you run it the submission works fine.

 

<form method="get" name="other_form" action="">
    Other form value:
    <input type="text" name="value" />
    <input type="submit" value="Submit" />
</form>

 

If you are having troubles I would probably guess that the problem is either with multiple forms named the same ("form1"), or the mark-up isn't correctly closing each form. That in effect would make it all part of form1 (assuming it came first).

 

Can you post the HTML?

  • 2 weeks later...

After gettingthis to work I found that even if a user tries to enter fewer than the required number of characters the form still submits on pressing the enter and then closing the alter box.  How do I prevent the form from being sent until they have or more than the required number in the search field ?

 

My JS file

function Minimum() {
        if (document.searchform.query.value.length < 3) {
            alert('Please enter a search query more than 3 characters.');
            return false;
        }
}

 

My HTML

<form name="searchform" action="cart.php" method="post">
<input title="Search" name="query" type="text" size="12" /><br /><input name="searchButton" type="submit" value="search" onclick="JavaScript:Minimum();" />
</form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.