validation script missing some combinations

J

jr

There are 4 + fields in a search form, bu, cart_id, zoneid, zonenm.
bu is required, search_bu in the form
however if the zoneid is searched on it requires the zonenm to make a
complete search.

testing:
1. click on BU & zoneid
asks for zonenm

2. click on cart_id
asks for bu this is okay but then
it asks for the zonenm ???????

The only time I want it to ask for the zonenm is when the zoneid is
checked.
I have the zonenm in a nested if that first asks whether there is a
value in the zoneid.
It shouldn't hit this 2nd if??

3. if I click zoneid
asks for the bu okay
put in bu
asks for zonenm okay

4. put in zone num
asks for bu
put in bu
it submits but without asking for the zoneid ????

thanks,
Janis r.





<script type="text/javascript">
function checkscript() {

if (document.forms[0].search_bu.value == '' ||
document.forms[0].search_bu.value == null) {
// something else is wrong
alert('The Business Unit is a required field!');
return false;
}
else if (document.forms[0].search_zoneid.value != '' ||
document.forms[0].search_zoneid.value != null ) {

if (document.forms[0].search_zonenm.value == '' ||
document.forms[0].search_zonenm.value == null ){
alert('You must input the zone number if you search on
zone ID');
return false;
}

}
else if (document.forms[0].search_zonenm.value != '' ||
document.forms[0].search_zonenm.value != null ) {

if (document.forms[0].search_zoneid.value == '' ||
document.forms[0].search_zoneid.value == null ){
alert('You must input the zone number if you search on
zone ID');
return false;
}

}
// if true you can submit the form

return true;
}
</script>
 
J

jr

jr wrote:

         document.forms[0].search_zoneid.value != null ) {

<snip>

This logical OR expression is certainly wrong. The possible values of
the - value - property of form controls are strings or null. If the -
value - of the field is null then that value is not equal to an empty
string and so the left hand side of the OR is true (making the whole
expression's result true), and if the - value - is the empty string then
the value is not equal to null, so the right hand side of the OR true
(making the whole expression's result true). All other possible values
are non-empty strings, which will not be equal to both null and the
empty string. The - if - is always true and its body will be executed
unconditionally.

A logical AND operation seems a better candidate; that the - value - not
be equal to the empty string AND it not be equal to null. That way you
only enter the - if - body when the value is a non-empty string value.

Richard.

Thanks, that is a good point and brings up a question, if the form
field value isn't posted by the user would you normally just have the
'' for empty?
maybe I don't need a null? How would it have a null if it is a search
form? In reality a database can have null values but I'm not even
sure how a form
field could have null values. Janis
 
J

jr

jr wrote:

         document.forms[0].search_zoneid.value != null ) {

<snip>

This logical OR expression is certainly wrong. The possible values of
the - value - property of form controls are strings or null. If the -
value - of the field is null then that value is not equal to an empty
string and so the left hand side of the OR is true (making the whole
expression's result true), and if the - value - is the empty string then
the value is not equal to null, so the right hand side of the OR true
(making the whole expression's result true). All other possible values
are non-empty strings, which will not be equal to both null and the
empty string. The - if - is always true and its body will be executed
unconditionally.

A logical AND operation seems a better candidate; that the - value - not
be equal to the empty string AND it not be equal to null. That way you
only enter the - if - body when the valu is a non-empty string value.

Richard.

Okay, I took out all of the && .....== NULL's
with just the test for == ''
It still does the same thing.

A problem still remains, if I enter the BU field by itself and click
submit
it is still asking me for the zone number.
The BU is the only required field. This is wrong.

However if I enter zoneID in the middle of the form like a bad user,
it correctly asks for the BU which is required,
and then asks correctly for the zonenum which is required if the
zoneid is called for.
There is a logic problem but it seems right. In the first if I ask
for the BU if there is no BU.
In the else/if I ask for the zonenm if there is no zonenzm if there IS
a zoneID .
Seems right.
thanks,
 
J

jr

jr wrote:

         document.forms[0].search_zoneid.value != null ) {

<snip>

This logical OR expression is certainly wrong. The possible values of
the - value - property of form controls are strings or null. If the -
value - of the field is null then that value is not equal to an empty
string and so the left hand side of the OR is true (making the whole
expression's result true), and if the - value - is the empty string then
the value is not equal to null, so the right hand side of the OR true
(making the whole expression's result true). All other possible values
are non-empty strings, which will not be equal to both null and the
empty string. The - if - is always true and its body will be executed
unconditionally.

A logical AND operation seems a better candidate; that the - value - not
be equal to the empty string AND it not be equal to null. That way you
only enter the - if - body when the value is a non-empty string value.

Richard.

I'm sorry I believe it was my mistake. The validation is working and
it is working with the if (field == '' || field == NULL)
If either it is null or empty it works. I still don't understand how
it could be null in a search form but I think it is just checking on
the first if ''
(empty).
thanks,
 
T

Thomas 'PointedEars' Lahn

Richard said:
Stefan said:
On Jul 7, 5:00 pm, Richard Cornford wrote:
jr wrote:
else if (document.forms[0].search_zoneid.value != '' ||
document.forms[0].search_zoneid.value != null ) {
[...]
I don't think an HTML form element can have a value of null
(i.e., exactly equal to null). I can't even think of a
situation where the value would not be a string.

The - value - properties of Netscape <= 4 SELECT elements were always
null (even after a selection had been maded).

But since it is known that the `value' property of those controls is not
interoperable (Netscape <= 4 notwithstanding), it should never be accessed
in the first place.

It is reasonable to assume that `document.forms[0].search_zoneid' (however
ill-advised that reference worm is, see the FAQ) refers to an object that
implements the HTMLInputElement interface. In that case it is more
important to make sure that both empty strings and whitespace values are
considered no-values, thus would cause form validation to fail:

if (!/\S/.test(….value))
{
// ...
return false;
}

However, the OP's description could be understood that they want to force
the user to enter the other value when the control for the one value is
clicked. Such an approach would certainly be ill-advised. Validation
should happen the moment before the form is submitted. It may happen before
that, but then the user must not be forced to enter a value (e.g., by
focusing the offending control automatically). The exception I would be
willing to accept is that if checking a checkbox or radiobutton required
another control to have a non-default value for the form to validate later,
that control may be focused automatically then, since that would help (and
not hinder) filling out the form using the keyboard.


PointedEars
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top