By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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)
Jul 20 '05 #2

P: n/a
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
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #3

P: n/a
Thanks to you both for the input
Jul 20 '05 #4

P: n/a

"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
Jul 20 '05 #5

P: n/a
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
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #6

P: n/a
OK. Many thanks. Got it sorted!!
Jul 20 '05 #7

P: n/a

"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
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.