validation has syntax errors [correction]

J

jr

This one seems like it should work, I don't get syntax errors but the
problem is it doesn't work.
The script doesn't validate anything. If it is correct then something
else is wrong otherwise it is not validating
any of the values, thanks,

function checkscript() {
if ( search_bu.value && search_error_flag) {
if ( zoneid.value && !zonenum.value ) {return 1 }

}

return 0;
 
D

Denis McMahon

function checkscript() {
if ( search_bu.value && search_error_flag) {
if ( zoneid.value && !zonenum.value ) {return 1 }
}

return 0;

I hope you had a "}" after the return 0?

I'm sure you're fully aware, having been told many times, that you can't
just use the field name or id to identify a field in javascript. You
have to translate that information somewhere into something the code
understands.

In fact, this is a fundamental concept that you should understand by now.

Irrespective of whether the logical flow of your code works (and as I
don't fully understand what you're trying to check, I can't verify
whether it will or not) I have no idea what the one variable and three
objects you're testing here are.

There are two ways to address objects relating to input elements in a
form from within javascript. One, which some people now consider
outdated, is as a named property of the parent form object, and the
other is using the element id. If using the named property of form
method, then you also need to identify the form, either as a member of
the document's forms collection, or as a named property of the document.

I have seen code that combines using the id of the form and then
referring to an input as a named property of that form, which is to me
illogical, as in my opinion if you're going to use element id attributes
you might as well use id at the input element level.

Once you have identified the object that relates to an input element on
a form, there is as one and only one common way to get the content of
that element.

Rgds

Denis McMahon
 
J

jr

I hope you had a "}" after the return 0?

I'm sure you're fully aware, having been told many times, that you can't
just use the field name or id to identify a field in javascript. You
have to translate that information somewhere into something the code
understands.

In fact, this is a fundamental concept that you should understand by now.

Irrespective of whether the logical flow of your code works (and as I
don't fully understand what you're trying to check, I can't verify
whether it will or not) I have no idea what the one variable and three
objects you're testing here are.

There are two ways to address objects relating to input elements in a
form from within javascript. One, which some people now consider
outdated, is as a named property of the parent form object, and the
other is using the element id. If using the named property of form
method, then you also need to identify the form, either as a member of
the document's forms collection, or as a named property of the document.

I have seen code that combines using the id of the form and then
referring to an input as a named property of that form, which is to me
illogical, as in my opinion if you're going to use element id attributes
you might as well use id at the input element level.

Once you have identified the object that relates to an input element on
a form, there is as one and only one common way to get the content of
that element.

Rgds

Denis McMahon

okay, that makes sense so if I do this ele.search_bu.value, I should
do ele= document.form[0], I think that was what was missing,
thanks, you are right I should have seen it.
 
J

jr

I hope you had a "}" after the return 0?

I'm sure you're fully aware, having been told many times, that you can't
just use the field name or id to identify a field in javascript. You
have to translate that information somewhere into something the code
understands.

In fact, this is a fundamental concept that you should understand by now.

Irrespective of whether the logical flow of your code works (and as I
don't fully understand what you're trying to check, I can't verify
whether it will or not) I have no idea what the one variable and three
objects you're testing here are.

There are two ways to address objects relating to input elements in a
form from within javascript. One, which some people now consider
outdated, is as a named property of the parent form object, and the
other is using the element id. If using the named property of form
method, then you also need to identify the form, either as a member of
the document's forms collection, or as a named property of the document.

I have seen code that combines using the id of the form and then
referring to an input as a named property of that form, which is to me
illogical, as in my opinion if you're going to use element id attributes
you might as well use id at the input element level.

Once you have identified the object that relates to an input element on
a form, there is as one and only one common way to get the content of
that element.

Rgds

Denis McMahon

I tried it like this and it still doesn't fire. there are 4 fields in
a search form. The first 2 required. The zonenm is only required if
there is no zoneid. IT seems like it should work. Rgds, Janis
function checkscript() {
// If bu and error_flag have a value return 0, if zoneid has a value
without zonenm proceed to next check
var ele=document.forms[0];
if (ele.search_bu.value && ele.search_error_flag.value ) {
// If zoneid has a value and zonenm does not have a value
return false
if (ele.search_zoneid.value && !ele.search_zonenm.value) {
return 1;
} else {
// if zoneid doesn't have a value, or
// if both zoneid and zonenm have values
return 0;
}
// If either bu or error_flag (or both) don't have a value
} else {
return 1;
}
}

</script>
Regards,
Janis
 
G

Gregor Kofler

Am 2010-08-03 17:34, schrieb jr:
okay, that makes sense so if I do this ele.search_bu.value, I should
do ele= document.form[0], I think that was what was missing,
document.form*s*[0].elements["search_blu"];

thanks, you are right I should have seen it.

Erm...

Gregor
 
R

Richard Cornford

I hope you had a "}" after the return 0?

I'm sure you're fully aware, having been told many times, that
you can't just use the field name or id to identify a field
in javascript.

One of the main reasons that people are continually told that they
shouldn't use a field's name or ID to identify a filed is that in many
cases you can, and so for some value of 'works' it does 'work'. The
problem with it is that there are some environments/contexts were it
won't work and so is neither reliable nor cross-browser.
You have to translate that information somewhere into something
the code understands.

In fact, this is a fundamental concept that you should
understand by now.

Irrespective of whether the logical flow of your code
works (and as I don't fully understand what you're trying
to check, I can't verify whether it will or not) I have no
idea what the one variable and three objects you're testing
here are.

No, the javascript code without the mark-up that creates the DOM with
which it interacts has relatively little meaning.
There are two ways to address objects relating to input
elements in a form from within javascript.

I can think of at least 6 obvious ways, and that means there will
probably be a dozen more.
One, which some people now consider outdated, is as a named
property of the parent form object,
and the other is using the element id.

That would be a vague distinction as a property of the FORM element
may exist with a name that corresponds with the ID of the element.
Presumably you mean accessing the form control using its ID with -
documentGetElementById -.

The W3C HTML DOM offers the - elements - property of the FORM element
as a collection of contained form control elements, that can be
integer indexed or referenced by name and/or ID. Being both DOM
standard and back-compatible with every scriptable web browser that
has ever known what a form is, this should be the preferred method of
accessing form controls.
If using the named property of form method, then you also
need to identify the form,
either as a member of the document's forms collection, or
as a named property of the document.

From a FORM element's - onsubmit - listener the form can be
anonymously referenced using the - this - keyword, and from the
intrinsic event listeners of form controls elements the - this.form -
will refer to the containing FROM element. Generally form validation
will involve one of these so there is normally no reason for the form
having a name/ID or there being any interest/reliance on the FORM's
index in the - document.forms - collection.
I have seen code that combines using the id of the form
and then referring to an input as a named property of that
form, which is to me illogical,

I don't see why. Strict HTML 4 does not allow FORM elements to have
NAME attributes, so there they cannot be referenced by name, and from
controls need to have NAME attributes if they are to be
'successful' (their values submitted to the server) so without some
other need (such as cross-referencing LABEL elements) adding ID
attributes to them could be superfluous.
as in my opinion if you're going to use element id
attributes you might as well use id at the input element
level.

An ID should be unique to the document, but a document could contain
multiple forms, and different forms may contain fields that
(logically) should have the same name.
Once you have identified the object that relates to an
input element on a form, there is as one and only one
common way to get the content of that element.

At least if you disregard radio buttons and checkboxes (neither of
which have "content" and their - value - properties are rarely of
primary interest while validating).

Richard.
 
J

jr

One of the main reasons that people are continually told that they
shouldn't use a field's name or ID to identify a filed is that in many
cases you can, and so for some value of 'works' it does 'work'. The
problem with it is that there are some environments/contexts were it
won't work and so is neither reliable nor cross-browser.




No, the javascript code without the mark-up that creates the DOM with
which it interacts has relatively little meaning.


I can think of at least 6 obvious ways, and that means there will
probably be a dozen more.


That would be a vague distinction as a property of the FORM element
may exist with a name that corresponds with the ID of the element.
Presumably you mean accessing the form control using its ID with -
documentGetElementById -.

The W3C HTML DOM offers the - elements - property of the FORM element
as a collection of contained form control elements, that can be
integer indexed or referenced by name and/or ID. Being both DOM
standard and back-compatible with every scriptable web browser that
has ever known what a form is, this should be the preferred method of
accessing form controls.


From a FORM element's - onsubmit - listener the form can be
anonymously referenced using the - this - keyword, and from the
intrinsic event listeners of form controls elements the - this.form -
will refer to the containing FROM element. Generally form validation
will involve one of these so there is normally no reason for the form
having a name/ID or there being any interest/reliance on the FORM's
index in the - document.forms - collection.


I don't see why. Strict HTML 4 does not allow FORM elements to have
NAME attributes, so there they cannot be referenced by name, and from
controls  need to have NAME attributes if they are to be
'successful' (their values submitted to the server) so without some
other need (such as cross-referencing LABEL elements) adding ID
attributes to them could be superfluous.


An ID should be unique to the document, but a document could contain
multiple forms, and different forms may contain fields that
(logically) should have the same name.


At least if you disregard radio buttons and checkboxes (neither of
which have "content" and their - value - properties are rarely of
primary interest while validating).

Richard.

thanks,
 
D

Denis McMahon

I tried it like this and it still doesn't fire. there are 4 fields in
a search form. The first 2 required. The zonenm is only required if
there is no zoneid. IT seems like it should work. Rgds, Janis
function checkscript() {
// If bu and error_flag have a value return 0,
// if zoneid has a value without zonenm proceed
// to next check
var ele=document.forms[0];
if (ele.search_bu.value && ele.search_error_flag.value ) {
// If zoneid has a value and zonenm does
// not have a value return false
if (ele.search_zoneid.value && !ele.search_zonenm.value) {
return 1;
} else {
// if zoneid doesn't have a value, or
// if both zoneid and zonenm have values
return 0;
}
// If either bu or error_flag (or both) don't have a value
} else {
return 1;
}
}

Ok, the function as presented appears, as far as I can tell, to match
the comments associated with it.

How are you calling the function? I would look at the opening tag of the
form element, and the submit element inside the form.

Are there any other forms on the page? If so, are you referring to the
correct form? It might be better, in the case of multiple forms, to use
a more explicit identification of the form. At present, you are assuming
that the form is form[0]. It may not be safe, if there are multiple
forms, to assume that that the forms array holds them in a predefined order.

If you give the form a name, you can use that name in your definition of
"ele".

Rgds

Denis McMahon
 
J

jr

I tried it like this and it still doesn't fire.  there are 4 fields in
a search form.  The first 2 required.  The zonenm is only required if
there is no zoneid.  IT seems like it should work.  Rgds, Janis
function checkscript() {
// If bu and error_flag have a value return 0,
// if zoneid has a value without zonenm proceed
// to next check
   var ele=document.forms[0];
    if (ele.search_bu.value  && ele.search_error_flag.value ) {
      // If zoneid has a value and zonenm does
      // not have a value return false
      if (ele.search_zoneid.value && !ele.search_zonenm.value) {
        return 1;
      } else {
        // if zoneid doesn't have a value, or
        // if both zoneid and zonenm have values
        return 0;
      }
    // If either bu or error_flag (or both) don't have a value
    } else {
      return 1;
    }
  }

Ok, the function as presented appears, as far as I can tell, to match
the comments associated with it.

How are you calling the function? I would look at the opening tag of the
form element, and the submit element inside the form.

Are there any other forms on the page? If so, are you referring to the
correct form? It might be better, in the case of multiple forms, to use
a more explicit identification of the form. At present, you are assuming
that the form is form[0]. It may not be safe, if there are multiple
forms, to assume that that the forms array holds them in a predefined order.

If you give the form a name, you can use that name in your definition of
"ele".

Rgds

Denis McMahon

This is the submit button in the view source:
<td colspan='13' align='center'><input type='submit' name='submit1'
value = ' Search'>

This is the form post in view source:
<form action='/tools/cart_inventory/cart_order_review.php'
onSubmit='return checkscript();' method='post'>

there is only one submit button, a search form. There are two sql's in
an if/else but only one search form and one result set. I called it
submit1 but it doesn't work if I call it just submit. rgds, Janis
 
J

jr

I tried it like this and it still doesn't fire.  there are 4 fields in
a search form.  The first 2 required.  The zonenm is only required if
there is no zoneid.  IT seems like it should work.  Rgds, Janis
function checkscript() {
// If bu and error_flag have a value return 0,
// if zoneid has a value without zonenm proceed
// to next check
   var ele=document.forms[0];
    if (ele.search_bu.value  && ele.search_error_flag.value ) {
      // If zoneid has a value and zonenm does
      // not have a value return false
      if (ele.search_zoneid.value && !ele.search_zonenm.value) {
        return 1;
      } else {
        // if zoneid doesn't have a value, or
        // if both zoneid and zonenm have values
        return 0;
      }
    // If either bu or error_flag (or both) don't have a value
    } else {
      return 1;
    }
  }

Ok, the function as presented appears, as far as I can tell, to match
the comments associated with it.

How are you calling the function? I would look at the opening tag of the
form element, and the submit element inside the form.

Are there any other forms on the page? If so, are you referring to the
correct form? It might be better, in the case of multiple forms, to use
a more explicit identification of the form. At present, you are assuming
that the form is form[0]. It may not be safe, if there are multiple
forms, to assume that that the forms array holds them in a predefined order.

If you give the form a name, you can use that name in your definition of
"ele".

Rgds

Denis McMahon

I also tried onClick,
 
T

Tim Streater

jr said:
there is only one submit button, a search form. There are two sql's in
an if/else but only one search form and one result set. I called it
submit1 but it doesn't work if I call it just submit. rgds, Janis

Can you explain what you mean by: "There are two sql's in an if/else but
only one search form and one result set." Where are they?
 
D

Denis McMahon

This is the submit button in the view source:
<td colspan='13' align='center'><input type='submit' name='submit1'
value = ' Search'>

This is the form post in view source:
<form action='/tools/cart_inventory/cart_order_review.php'
onSubmit='return checkscript();' method='post'>

I believe that onsubmit expects a boolean value, but you are returning a
numeric value. Is it possible that your return values are not being
interpreted as you expect?

Perhaps it would be better to actually return true or false from your
validation function, instead of 1 or 0.

Rgds

Denis McMahon
 
J

jr

I believe that onsubmit expects a boolean value, but you are returning a
numeric value. Is it possible that your return values are not being
interpreted as you expect?

Perhaps it would be better to actually return true or false from your
validation function, instead of 1 or 0.

Rgds

Denis McMahon

I will try that thanks,
 
J

jr

Can you explain what you mean by: "There are two sql's in an if/else but
only one search form and one result set." Where are they?

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

Sure it was a cross over from mysql/php I probably didn't need to
mention it here on this list but there is an if/then that pulls
different rows of records depending on the values selected to search
on in the search box. It so happens if there is an error then there
is no match key and the sql is only on the one table of records so it
displays different results if there is amatch key then all the columns
are returned.
 
L

Lasse Reichstein Nielsen

jr said:
This one seems like it should work, I don't get syntax errors but the
problem is it doesn't work.

"Doesn't work" isn't a very good error description.
I'm going to harp on this again at the end, please stay tuned :)
The script doesn't validate anything. If it is correct then something
else is wrong otherwise it is not validating
any of the values, thanks,

function checkscript() {
if ( search_bu.value && search_error_flag) {
if ( zoneid.value && !zonenum.value ) {return 1 }

}

return 0;

There is far too little context here to say what's not working.
We can't ses what the variables "search_bu", "search_error_flag" etc.
refer to (if anything).

I'm assuming this is a form validation. In that case, I recommend
passing the form as a parameter to the function and make explicit
lookups in its elements array. E.g.

<form action="..." onsubmit="return checkscript(this);">
<input type="text" name="search_bu" ...
</form>
<script>
function checkscript(form) {
var elems = form.elements;
return elems['search_bu'].value != ""
&& elems['search_error_flag'].checked
&& elems['zoneid'].value != ""
&& Number(elems['zonenum'].value) != 0;
// You must return *false* to cancel the submit,
// 0 isn't good enough.
}
</script>

Here I'm guessing that the input control with name 'search_error_flag'
is a checkbox, and the rest are text inputs of some sort, with the
content of 'zonenum' being interpreted as a number.



In general (and that's not just for you), to get good help with a
problem you need to provide enough information for the reader to
reproduce, recognize and repair the problem. That means:

Reproduce: What did you do - all the code necessary to try it, but
please cut it down to a small self-contained example that still
exhibits the problem. If it's more than a few lines, a link to a
page is also good.
Recognize: What happened - be specific. "Nothing happened" isn't
as good as "there was no visible change on the page".
Repair: What did you expect to happen - again, be specific.

Without either of these, you will have people either guessing (which
is likely to be a waste of both their time and your chance of getting
a good answer) or saying an ironic "You did something wrong, try
something else".

Best of luck
/L
 
D

Dr J R Stockton

In comp.lang.javascript message <3ade0983-7b2e-4351-87f9-4cb9b5f51831@f6
g2000pro.googlegroups.com>, Tue, 3 Aug 2010 08:44:02, jr
I tried it like this and it still doesn't fire. there are 4 fields in
a search form. The first 2 required. The zonenm is only required if
there is no zoneid. IT seems like it should work. Rgds, Janis
function checkscript() {
// If bu and error_flag have a value return 0, if zoneid has a value
without zonenm proceed to next check
var ele=document.forms[0];
if (ele.search_bu.value && ele.search_error_flag.value ) {
// If zoneid has a value and zonenm does not have a value
return false
if (ele.search_zoneid.value && !ele.search_zonenm.value) {
return 1;
} else {
// if zoneid doesn't have a value, or
// if both zoneid and zonenm have values
return 0;
}
// If either bu or error_flag (or both) don't have a value
} else {
return 1;
}
}

</script>


If you want to make helping you easy, you MUST post easily-readable,
easily-testable code.

Leave a gap between code and non-code.

Don't post a mismatched </script> or similar.

Use true & false for Booleans, not 1 & 0.

DO NOT ALLOW your posting agent to line-wrap code; preferably write code
with lines of 72 characters or fewer. Posted code must be RECEIVED as
executable code.

And "else" is rarely needed after "return", and IMHO should not be used
where not needed.

Read the FAQ.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Tue, 3 Aug 2010 14:44:29, Denis McMahon <[email protected]
m> posted:
There are two ways to address objects relating to input elements in a
form from within javascript. One, which some people now consider
outdated, is as a named property of the parent form object, and the
other is using the element id. If using the named property of form
method, then you also need to identify the form, either as a member of
the document's forms collection, or as a named property of the document.


To me, using Form.name seems far more structured than using an ID.

Query : is there an array or collection of IDs, as there is of anchors?
None of my guessed identifiers for it was accepted, and without an
identifier it's hard to look up,
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top