Javascript causes entire script to fail on web page

T

Taz Lake

Any JavaScript folks want to take a shot at this? I have a larger
JavaScript that is doing form validation (I can provide it if needed).
But this particular piece of code causes the entire script to fail
(i.e., not execute) and allows the form to be submitted. As soon as I
remove it, the validation works fine.

if (employment_status.selectedIndex == 3 ||
employment_status.selectedIndex == 4 ||
employment_status.selectedIndex == 5 ||
employment_status.selectedIndex == 6) && (contract_end_date.value ==
"")
{
alert ("You must select an end date for the new hire if they are a
contractor or temporary employee in order to submit the New Hire
Form.")
employment_status.focus()
return false
}

Any help is appreciated as I've looked at this quite and bit nothing
jumps out. Please reply to the board so all can benefit.

Thanks,
Taz
 
L

Lee

Taz Lake said:
Any JavaScript folks want to take a shot at this? I have a larger
JavaScript that is doing form validation (I can provide it if needed).
But this particular piece of code causes the entire script to fail
(i.e., not execute) and allows the form to be submitted. As soon as I
remove it, the validation works fine.

if (employment_status.selectedIndex == 3 ||
employment_status.selectedIndex == 4 ||
employment_status.selectedIndex == 5 ||
employment_status.selectedIndex == 6) && (contract_end_date.value ==
"")

Whatever browser you're using should be pointing out the syntax
error in that "if" statement.

The entire expression to be evaluated must be contained in parentheses.
You have two sets of parentheses joined by an && operator.
Add an outer pair, enclosing the entire expression.
 
L

Lasse Reichstein Nielsen

(e-mail address removed) (Taz Lake) writes:

I assume "employment_status" is a declared variable that points to
a select element.
if (employment_status.selectedIndex == 3 ||
^ parenthesis begin
employment_status.selectedIndex == 4 ||
employment_status.selectedIndex == 5 ||
employment_status.selectedIndex == 6) && (contract_end_date.value ==
parenthesis end-^ ^^-syntax error

This would have been caught by JSLint:
<URL: http://www.crockford.com/javascript/jslint.html >
It's error message is:
4) : error at character 39: Expected '{' and instead saw '&&'.

/L
 
M

Mick White

Taz said:
Any JavaScript folks want to take a shot at this? I have a larger
JavaScript that is doing form validation (I can provide it if needed).
But this particular piece of code causes the entire script to fail
(i.e., not execute) and allows the form to be submitted. As soon as I
remove it, the validation works fine.

if (employment_status.selectedIndex == 3 ||
employment_status.selectedIndex == 4 ||
employment_status.selectedIndex == 5 ||
employment_status.selectedIndex == 6) && (contract_end_date.value ==
"")
{
alert ("You must select an end date for the new hire if they are a
contractor or temporary employee in order to submit the New Hire
Form.")
employment_status.focus()
return false
}

Any help is appreciated as I've looked at this quite and bit nothing
jumps out. Please reply to the board so all can benefit.

Thanks,
Taz

employment_status= [valid reference here]
contract_end_date=[valid reference here]
// valid reference should include the document,
// the form, and the form control, for example:
// document["formName'].elements["selectName"]


if (employment_status.selectedIndex >2 &&
employment_status.selectedIndex<7 && !contract_end_date.value){

alert ("In order to submit the New Hire Form, you must select an end
date for the new hires if they are contractors or temporary employees.")
employment_status.focus();
return false;
}

Mick
 
T

Taz Lake

Thanks to everyone for your assistance and suggestions. Apparently,
the most obvious errors are the most difficult to troubleshoot ;-)

I'm using Firebird instead of something like IE which will provide you
with JavaScript notifications in a pop-up window. I just typed
"javascript:" in the browser window and it reported the error (just
like Netscape, duh).

Thanks,
Taz
 
T

Thomas 'PointedEars' Lahn

Taz said:
I'm using Firebird instead of something like IE which will provide you
with JavaScript notifications in a pop-up window. I just typed
"javascript:" in the browser window and it reported the error (just
like Netscape, duh).

The Web Developer Extension will not only install helpful bookmarklets
accessible via a toolbar but also show an icon in that toolbar to
indicate if script errors/warnings have occurred. You can always
click the icon to display the JavaScript console. Available for
Mozilla/5.0 Seamonkey and Firefox.

<http://chrispederick.myacen.com/work/firefox/webdeveloper/download/>


HTH

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top