How do I - add an 'if is empty then..warning...else go"

Discussion in 'Javascript' started by Richard, Nov 29, 2003.

  1. Richard

    Richard Guest

    How do I prevent a submit action from occurring if the select
    box has no value.
    The box is for a database search where the user must enter
    a keyword. Leaving the box empty triggers a blanket search
    of the database and uses lots of processing time and gives too
    many results. Is there something I can add to the submit button
    to trap an empty select box and trigger a warning that something
    must be entered?
    <input type=hidden name="action" value="searchdbdisplay">
    <input type="submit" value="Search">

    Thanks in advance
     
    Richard, Nov 29, 2003
    #1
    1. Advertising

  2. Richard

    Evertjan. Guest

    Richard wrote on 29 nov 2003 in comp.lang.javascript:

    > How do I prevent a submit action from occurring if the select
    > box has no value.


    <form action="/test.html">
    <SELECT id="ha" name="ha">
    <OPTION value="">---- choose ----
    <OPTION value="-1-">one
    <OPTION value="-5-">bu
    <OPTION value="-10-">bip
    <OPTION value="-15-">boh
    <OPTION value="-999-">blah
    </SELECT>
    <input type="submit" value="Search"
    onclick="return document.getElementById('ha').value.length>0">
    </form>



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 29, 2003
    #2
    1. Advertising

  3. Richard wrote on 29 Nov 2003:

    > How do I prevent a submit action from occurring if the select
    > box has no value.
    > The box is for a database search where the user must enter
    > a keyword. Leaving the box empty triggers a blanket search
    > of the database and uses lots of processing time and gives too
    > many results. Is there something I can add to the submit button
    > to trap an empty select box and trigger a warning that something
    > must be entered?
    > <input type=hidden name="action" value="searchdbdisplay">
    > <input type="submit" value="Search">


    Add this intrinsic event to your FORM element:

    <FORM ... onsubmit="return validateSearch(this)">

    ....this META statement to the document HEAD:

    <META http-equiv="Content-Script-Type" content="text/javascript">

    ....and this function:

    <SCRIPT type="text/javascript">
    function validateSearch( form ) {
    // If the keywords field is not empty (length > 0), continue
    if ( form.elements['keywords'].value.length ) return true;

    // ...otherwise, alert the user and abort the search
    window.alert('Please enter a keyword before starting a search');
    return false;
    }
    </SCRIPT>

    Can you not check this server-side? It would be more reliable as a
    user with JavaScript disabled will not be affected by this check.
    However, you could still do this, even with the server-side check, as
    the user wouldn't have to have to wait for the server response.

    Mike

    --
    Michael Winter
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 29, 2003
    #3
  4. Richard

    Richard Guest

    Re: How do I - add an 'if is empty then..warning...else go" - thanks

    Thanks to you both for the input
     
    Richard, Nov 30, 2003
    #4
  5. Richard

    Richard Guest

    "Michael Winter" wrote in message

    > Richard wrote on 29 Nov 2003:
    >
    > > How do I prevent a submit action from occurring if the select
    > > box has no value.


    > Add this intrinsic event to your FORM element:
    >
    > <FORM ... onsubmit="return validateSearch(this)">
    >
    > ...this META statement to the document HEAD:
    >
    > <META http-equiv="Content-Script-Type" content="text/javascript">
    >
    > ...and this function:
    >
    > <SCRIPT type="text/javascript">
    > function validateSearch( form ) {
    > // If the keywords field is not empty (length > 0), continue
    > if ( form.elements['keywords'].value.length ) return true;
    >
    > // ...otherwise, alert the user and abort the search
    > window.alert('Please enter a keyword before starting a search');
    > return false;
    > }
    > </SCRIPT>


    Hi, tried this, and the other suggestion. Would you pls be
    kind enough to look at the page. It does NOT currently
    have either suggestion in it.
    http://www.missingfriends.net/search.html
    I dont know if some of the hidden fields, required by the
    search script would be blocking the desired action.
    Regards
     
    Richard, Nov 30, 2003
    #5
  6. Richard wrote on 30 Nov 2003:

    > Hi, tried this, and the other suggestion. Would you pls be
    > kind enough to look at the page. It does NOT currently
    > have either suggestion in it.
    > http://www.missingfriends.net/search.html
    > I dont know if some of the hidden fields, required by the
    > search script would be blocking the desired action.


    What exactly is the problem? When I tried it (I made a local copy of
    your page and edited that), it worked perfectly. Below is the form I
    used:

    <form method="POST"
    action="http://www.missingfriends.net/cgi-bin/search.cgi"
    onsubmit="return validateSearch(this)">
    <input type=hidden name="template" value="search-summary.html">
    <input type=hidden name="dbname" value="messages.dat">
    <input type=hidden name="wordmatch-keywords" value="yes">
    <font face="arial" size="2">The whole database is searched for the
    specified keywords. The default keyword separator is a comma
    character. If you want to search a number of keyphrases, you may use
    either "++" (the <b>OR</b> operator) or "&&" (the <b>AND</b>
    operator) </font><br>

    <b><font face="arial" size=-1>Enter Keywords</font></b><font
    face="arial" size=-1>
    <input type="hidden" name="keywords_separator_is_comma" value="yes">
    <input name="keywords" size=40 maxlength=80>
    <input type="hidden" name="action" value="searchdbdisplay">
    <input type="submit" value="Search">

    </font></form>

    I added the script in my previous response just above this form
    (though it works, and would be better placed, in the document HEAD).
    This is with both IE6 and Opera, so could you please explain what's
    wrong? Did you just alter the first form, or all of them? It
    shouldn't actually matter as you use a 'keywords' field in all of
    them, and the script doesn't depend on a specific form. Are there any
    error messages, or does it just not work as expected. If it is the
    latter, please explain what does happen.

    Mike

    --
    Michael Winter
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 30, 2003
    #6
  7. Richard

    Richard Guest

    SORTED!

    OK. Many thanks. Got it sorted!!
     
    Richard, Nov 30, 2003
    #7
  8. Richard

    Richard Guest

    "Michael Winter" wrote in message
    > What exactly is the problem?


    I - am the problem - got to learn the difference
    between a colon and a semi-colon!

    Back to typing school!!!

    Thanks again
     
    Richard, Nov 30, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    25
    Views:
    689
    MonkeeSage
    Oct 1, 2006
  2. kj
    Replies:
    15
    Views:
    582
    Lawrence D'Oliveiro
    May 23, 2009
  3. A
    Replies:
    8
    Views:
    820
    James Kanze
    Aug 28, 2010
  4. Replies:
    0
    Views:
    316
  5. Replies:
    15
    Views:
    234
Loading...

Share This Page