From: jr on
On Jul 7, 5:00 pm, "Richard Cornford" <Rich...(a)litotes.demon.co.uk>
wrote:
> jr wrote:
>
> <snip>> else if (document.forms[0].search_zoneid.value != '' ||
> >          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
From: jr on
On Jul 7, 5:00 pm, "Richard Cornford" <Rich...(a)litotes.demon.co.uk>
wrote:
> jr wrote:
>
> <snip>> else if (document.forms[0].search_zoneid.value != '' ||
> >          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,
From: Stefan Weiss on
On 08/07/10 02:06, jr wrote:
> On Jul 7, 5:00 pm, "Richard Cornford" <Rich...(a)litotes.demon.co.uk>
> wrote:
>> jr wrote:
>> <snip>> else if (document.forms[0].search_zoneid.value != '' ||
>> > document.forms[0].search_zoneid.value != null ) {
>>
>> <snip>
>>
>> This logical OR expression is certainly wrong

[...]

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

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. In any case, you'll probably want to handle
them the same way, so something like "if (!...value)" may be more
appropriate.

Richard has already explained the reason why your validation gave
unexpected results. To make things easier, you could use something like
this:

function checkscript() {
var ele = document.forms[0].elements;
if (!ele.search_bu.value) {
alert('The Business Unit is a required field!');
return false;
} else if (ele.search_zoneid.value && !ele.search_zonenm.value) {
alert('Zone ID but no zone number');
return false;
} else if (ele.search_zonenm.value && !ele.search_zoneid.value) {
alert('Zone number but no zone ID') {
return false;
}
return true;
}

Just be aware that if one of the fields (maybe a select dropdown) has a
default value of "0", this value will evaluate to true.


--
stefan
From: jr on
On Jul 7, 5:00 pm, "Richard Cornford" <Rich...(a)litotes.demon.co.uk>
wrote:
> jr wrote:
>
> <snip>> else if (document.forms[0].search_zoneid.value != '' ||
> >          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,
From: Richard Cornford on
Stefan Weiss wrote:
> On 08/07/10 02:06, jr wrote:
>> On Jul 7, 5:00 pm, Richard Cornford wrote:
>>> jr wrote:
>>> <snip
>>>> else if (document.forms[0].search_zoneid.value != '' ||
>>>> document.forms[0].search_zoneid.value != null ) {
>>>
>>> <snip>
>>>
>>> This logical OR expression is certainly wrong
>
> [...]
>
>> 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.
>
> 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.
<snip>

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

Richard.