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

R

Richard

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
 
E

Evertjan.

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>
 
M

Michael Winter

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
 
R

Richard

"Michael Winter" wrote in message
Richard wrote on 29 Nov 2003:
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
 
M

Michael Winter

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
 
R

Richard

"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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top