Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Javacsript enigma

See the following code:

alert("ac (4a) = " + ac);
			<!-- PSI's -->	
			if (tp != "PVB" && tp != "SVB" && tp != "AVB") {
			if (document.st.itcv1psi.value != "") {
				if (! isnumeric(document.st.itcv1psi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}	
			if (document.st.itcv2psi.value != "") {
				if (! isnumeric(document.st.itcv2psi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}
			if (document.st.itrvpsi.value != "") {
				if (! isnumeric(document.st.itrvpsi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}
			if (document.st.itaipsi.value != "") {
				if (! isnumeric(document.st.itaipsi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}
			if (document.st.itcvpsi.value != "") {
				if (! isnumeric(document.st.itcvpsi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}
			if (document.st.ftcv1psi.value != "") {
				if (! isnumeric(document.st.ftcv1psi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}	
			if (document.st.ftcv2psi.value != "") {
				if (! isnumeric(document.st.ftcv2psi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}
			if (document.st.ftrvpsi.value != "") {
				if (! isnumeric(document.st.ftrvpsi.value)) {
					alert("One or more PSI not numeric.");
					return false;
				}
			}
			}
			alert("ac (4b) = " + ac);

Open in new window


This is a small segment of the entire code; I am trying to figure out why later Javascript is not executed.

Note at the top of this code, the alert ac (4a) = . That alert shows and has the correct value of ac.

Note at the bottom the alert: alert("ac (4b) = " + ac); That never shows.

This tells me there is a Javascript error in between.

The value of tp is "DCD". I have run this in Firefox with Firebug on; shows nothing. Seems to me there MUST be a syntax error in the code shown; I cannot see it.

Can someone?
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
maybe you should use isNaN instead of isnumeric...

! isnumeric(document.st.ftcv2psi.value)

Open in new window


>>>

isNaN (document.st.ftcv2psi.value)

Open in new window


have a look at this demo
https://jsfiddle.net/cb0yq99e/
Avatar of Leonidas Dosas
"return" only makes sense inside a function. There is no function in your code.The return statement is inside to an if statement.
"return" only makes sense inside a function. There is no function in your code.The return statement is inside to an if statement.

probably it is just a piece of code from a function, dont think return is the problem...

isnumeric is giving problem, no such thing in javascript, unless op has a custom function somewhere...
Avatar of Richard Korts

ASKER

HainKurt. I have used the syntax ! isnumeric(document.st.ftcv2psi.value) for years. The isunumeric function & related are here:

function isdigit(test) {
		if (test == "0" || test == "1" || test == "2" ||
	    	test == "3" || test == "4" || test == "5" ||
	    	test == "6" || test == "7" || test == "8" ||
	    	test == "9" || test == ".")
			return true;
		else {
			return false;
	     }
	}
// Function to determine if a field is numeric
	function isnumeric(field) {
		len = field.length;
		for (i = 0; i < len; i++)
			if (isdigit(field.charAt(i)) == false) {
				return false;
			}
		return true;
	}

Open in new window


All of the code I initially supplied is inside the function chk_vals() which is referenced in the form statement "onsubmit=return chk_vals();"

I am trying to simplify the problem for you experts so you don't have to go through a TON of code.

I will try the Chrome F12.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still no issue with your code!

https://jsfiddle.net/bjobk46a/

I hardcoded

var ac = 0;
var tp = "X";

Open in new window


to make it run... probably they are defined somewhere else... o/w you would not get first alert message...
also I removed

<!-- PSI's -->	

Open in new window


maybe that one is causing problems...

comment on js should be like

// PSI's

Open in new window

Opening in Chrome, using F12 produces this: (in red, I assume red means bad)

Failed to load resource: the server responded with a status of 404 (Not Found)

This is the line of code in the <head> section: <link rel="stylesheet" href="/resources/demos/style.css">

This is used in every other page in this app (that has HTML), not sure what it should be but this page previously worked with that for at least 9 months (before other changes).

I removed the HTML style comment, no difference,

The variable ac is defined earlier, in this code that leads up to the function being run & the first alert which works:

ac = "";
agency = "<? print $_SESSION['form']; ?>";
tp = "<? print $t; ?>";
cancpg = "choose_cust.php";
<? if ($_SESSION['nr'] == 0) { ?>
	cancpg = "clear_sess.php";
<? } ?>	
		function chk_vals() {
				alert("ac = " + ac);

Open in new window

The only assignment is... ac = "";  That looks like it is always blank.
Dave,

ac is assigned in the form body depending upon the specific submit button pushed. In the case in question, it is "s".

I realize that is not self evident, sorry.

Years ago I took to the convention of ac = "" at the beginning so that Javascript did not complain of an undefined variable.

Richard
sorry what is not working now?
I put your code and created a demo and it works fine...
HainKurt,

The issue is when ac = "s", the form action is changed so that the form data is saved but NOT printed.

In the action program, I have pared it down to showing a get variable & exiting. It NEVER get's there

It is printing the form every time & is NOT executing the later alerts in the Javascript.

I guess I will have to use the old fashioned method of eliminating Javascript code in  big chunks until it works, then start putting it back in a piece at at time till it breaks.
if it is not running, then it means it is giving exception!
open in chrome, press F12 and do the same thing...
check console, you should see the error message

before that, are you getting any other alert? like "One or more PSI not numeric."?
if yes, then it is normal not to get the last alert, since you have return inside ifs, which exits from functions...
I did the chrome think, it does not work.

Nor does Firebug in Firefox.

I will start by removing ALL the code below the final alert that shows.

Thanks for your efforts.
Problem is not solved. I realize it's a tough one, I am not a novice at this stuff.

Points awarded for effort.