From: jr on
I have a search form in php and mysql.
There is only one required field the 'bu'.
The validation message doesn't display when I submit the search form
if I don't have a 'bu' selected? So something is wrong with the
javascript.
Also, if the user wants to search on zoneid then they must also input
a zonenm.
The bu is always required. How do I validate for if there is a zoneid
the zonenm is required?
The get_bu function just pulls up a value list of the bu's in the
database.
The form works for the search I just need the input validated.
thanks,
---javascript------


<script language='javascript'>
function validate_form(thisform) {

var sMsg='';
var sSel='N';
with (thisform) {
if ( bu.value=='' ) { sMsg+="No bu\r\n"; }

for (i = 1; i < bu.length; i++) {
if ( bu[i].selected) { sSel='Y'; }

}
if ( sSel=='N' ) { sMsg+="No BU selected\r\n"; }




if ( sMsg.length > 0 ) {
alert(sMsg);
return false;
}else{
return true;
}
}

}


</script>

---form-----
<form action='$me' method='post' onsubmit='return
validate_form(this);'>
<td><table cellpadding='3' cellspacing='0' border='0' style='border:
1px solid #CCCCCC' align='center'>\n
<th colspan='2'><span class='ast'>*</span>Search for Labels to Print\n
<tr>\n
<th align='right'><span class='ast'>*</span>BU\n
<td><select name='bu'>\n
get_bu()
<tr>\n
<th align='right'>Zone ID\n
<td><input type='text' name='search_zoneid' size='20'>\n
<tr>\n
<th align='right'>Zone Number\n
<td><input type='text' name='search_zonenm' size='20'>\n"
<tr>\n";
<th align='right'>NDC\n
<td><input type='text' name='search_nationalDrugCode' size='20'>\n
<tr>\n
<td align='center' colspan='2'><input type='submit' value='Search'>\n
</table>\n
<tr>\n
<td><span class='ast'>*</span> - <font size='-1'><i>Required</i>\n
</table>\n
<tr>\n
</form>
thanks,
From: Jukka K. Korpela on
jr wrote:

> I have a search form in php and mysql.

You should have posted a URL, as that would let us access the HTML document
that browsers and other web clients receive - they won't see your php code
for example.

> There is only one required field the 'bu'.
> The validation message doesn't display when I submit the search form
> if I don't have a 'bu' selected?

To the extent we can guess from the code snippet, 'bu' is a select element.
Depending on the code, on the browser, and possibly on the phase of the
moon, a select element may or may not be "successful" in HTML terms, i.e.
have a specific value and contribute to the form data set, when a user has
made no explicit selection. The select element may have an option that is
initially selected, as it has the SELECTED attribute, or because the browser
by defaults makes the first option pre-selected.

Moreover, your test
> if ( bu.value=='' ) { sMsg+="No bu\r\n"; }
is not a proper way of testing for the state of a select element. Use
selectedIndex instead. The really safe way is to include an option like
<option selected value="">Please select:</option>
and test whether the selectedIndex value matches the index of that option.

> How do I validate for if there is a zoneid
> the zonenm is required?

Assign id attributes to the textfields, e.g.
<input type='text' name='search_zoneid' size='20' id='zoneid'>
and use code like

if(document.getElementById('zoneid').value != '' &&
document.getElementById('zonenm').value == '') {
alert('Missing zone number.');
return false; }

You should also make your HTML markup valid. It is a pointless risk to throw
grossly malformed syntax soup at browsers, even though they often seem to
digest it. In particular, you now have a spurious <td> between the <form
....> tag and the <table ...> tag and a spurious <tr> between the </form> tag
and the </table> tag, Use a validator like
http://www.htmlhelp.com/validator/ (on the HTML document as served to web
clients, not on the PHP document!) to find syntax errors. Serious syntax
errors in HTML may result in unpleasant surprises in scripting and
otherwise.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

From: Thomas 'PointedEars' Lahn on
Jukka K. Korpela wrote:

> jr wrote:
>> How do I validate for if there is a zoneid the zonenm is required?
>
> Assign id attributes to the textfields, e.g.
> <input type='text' name='search_zoneid' size='20' id='zoneid'>
> and use code like
>
> if(document.getElementById('zoneid').value != '' &&
> document.getElementById('zonenm').value == '') {
> alert('Missing zone number.');

window.alert('...');

> return false; }

Unnecessary, and recommended against. Use the `elements' collection of the
`forms' collection instead, best by passing a reference to the form object
in the event listener:

<script type="text/javascript">
function validateForm(f)
{
var es = f.elements;
if (/\S/.test(es["zoneid"].value)
&& !/\S/.test(es["zonenm"].value))
{
return false;
}

return true;
}
</script>

...

<form ... onsubmit="return validateForm(this);">
...
</form>

IDs can be used with this in newer (W3C DOM Level 2-compliant) DOM
implementations, but names will suffice for all.

An accessible form would fall back to server-side validation, e.g. have
a sensible value for the `action' attribute of that `form' element.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee