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

asked on

Javascript not working?

see this code:
	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


and
pp6 = pp5.trim();
				alert("pp6 = " + pp6);
				pnl = pp6.length;
				alert("pnl = " + pnl);
				if (pnl > 11) {
					alert ("Invalid phone number.");
					return false;
				}
				if (! isnumeric(pp6)) {
					alert ("pp6 not numeric");
					alert ("Invalid phone number.");
					return false;
				}

Open in new window

In a specific case, pp5 is 6198559182

The alerts show pnl is 11 (impossible) and pp6 is NOT numeric (impossible).

What am I missing.

FYI, this was working a week or so ago; I am using Firefox, they just did a seemingly MAJOR update. I have not tried in Chrome. Could that be the problem? To my knowledge I have made no other changes (the date before I put in all the alerts was 11/21/17).
Avatar of Richard Korts
Richard Korts
Flag of United States of America image

ASKER

Must be Firefox; I tried in Chrome, worked fine.
Avatar of Dave Baldwin
Fooled you again.  The 'return' statements in the 'if' clauses are not valid and Firefox doesn't like that.  This works.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">
<!--
	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;
	}
	
pp5 = '6198559182';
pp6 = pp5.trim();
				alert("pp6 = " + pp6);
				pnl = pp6.length;
				alert("pnl = " + pnl);
				if (pnl > 11) {
					alert ("Invalid phone number.");
					//return false;
				}
				if (! isnumeric(pp6)) {
					alert ("pp6 not numeric");
					alert ("Invalid phone number.");
					//return false;
				}

// -->
</script>
</body>
</html>

Open in new window

In Firefox, you can press Ctl-Shift-J to bring up the error console and see what Firefox doesn't like.
Dave,

I have used, successfully, those functions isnumeric and isdigit for probably over 10 years.

Why would they stop working now in Firefox? Seems to work in Chrome.

Richard
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America 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
Ok, I see. I'm not at my main computer now, I stupidly copied those few lines out of a larger JavaScript function that is referenced by the onSubmit method of the form.

I always try to post the smallest possible code snippets so that I can to make it easy for the responders.

I'll post the whole function tomorrow.

All the evidence I see now says it's a Firefox problem.
Then frankly you're not looking.   Although Chrome will 'run' the code, it also flags the 'returns' in the 'if' blocks as 'Uncaught SyntaxError: Illegal return statement' .  Ctrl-Shift-I in Chrome brings up the Developer tools and you can see that statement in the 'Console'.
I think the question is where is this code defined
if (pnl > 11) {
	alert ("Invalid phone number.");
	return false;
}
if (! isnumeric(pp6)) {
	alert ("pp6 not numeric");
	alert ("Invalid phone number.");
	return false;
}

Open in new window

Is it in the global scope or inside a function - it appears to be snippet only so we could be blinded by lack of context.

What Dave is saying is that a return statement is not valid unless contained inside a function - the if statement bit is misleading. For a return to be valid it needs to be inside a function.

Is the code above inside a function and if so can we see the full function.
Just for giggles I put the code above in a function called test and called that.

I get two alerts
The first says
pp6 = 6198559182

Open in new window

The second says
pnl = 10

Open in new window

Those are the only outputs - no errors
Here is what my function looks like
function test() {
	pp5 = '6198559182';
	var pp6 = pp5.trim();
	alert("pp6 = " + pp6);
	var pnl = pp6.length;
	alert("pnl = " + pnl);
	if (pnl > 11) {
		alert ("Invalid phone number.");
		return false;
	}
	if (! isnumeric(pp6)) {
		alert ("pp6 not numeric");
		alert ("Invalid phone number.");
		return false;
	}
}

Open in new window

Here is the full code of the function chk_vals(). This is referenced by a onSubmit="return chk_vals()" in the form statement.

Sorry it is so long.

I removed the debug alerts that were between lines 45 & 46 and between lines 49 & 50.

	function chk_vals() {
			if (ac == "l") {
				jj = confirm("Log Out will lose all data entered. OK?");
				if (! jj) {
					return false;
				}
				document.st.action = "logout.php";
				return true;
			}	
			if (ac == "c") {
				jj = confirm("Cancel will lose all data entered. OK?");
				if (! jj) {
					return false;
				}	
				document.st.action = cancpg;
				return true;
			}	
			if (ac == "f") {
				jj = confirm("Return to forms list will lose all data entered. OK?");
				if (! jj) {
					return false;
				}	
				document.st.action = cancpg;
				return true;
			}	
			if (document.st.pn.value == "" || document.st.padr.value == "" || document.st.cityf.value == "" || document.st.zip.value == "") {
				alert("Minimum of property name, address, city & zip required.");
				return false;
			}
			if (document.st.cityf.value != document.st.city.value) {
				document.st.city.value = document.st.cityf.value;
			}
			if (document.st.prop_phone.value != "") {
				pp = document.st.prop_phone.value;
				pp1 = pp.replace(" ", "");
				pp1 = pp1.replace(" ", "");
				pp2 = pp1.replace(".", "");
				pp2 = pp2.replace(".", "");
				pp3 = pp2.replace("-", "");
				pp3 = pp3.replace("-", "");
				pp4 = pp3.replace("(", "");
				pp5 = pp4.replace(")", "");
				pp6 = pp5.trim();
				pnl = pp6.length;
				if (pnl > 11) {
					alert ("Invalid phone number.");
					return false;
				}
				if (! isnumeric(pp6)) {
					alert ("Invalid phone number.");
					return false;
				}
			}	
			if(typeof document.st.keymap1 != "undefined" && document.st.keymap1.value != "") {
				if (! isnumeric(document.st.keymap1.value)) {
					alert("Invalid non-numeric beginning of Key Map");
					return false;
				}
			}
			<!-- Zip Code -->
			zip = document.st.zip.value;
			if (! isnumeric(zip)) {
				alert("Invalid non-numeric zip code.");
				return false;
			}
			if( zip.length != 5) {
				alert("Invalid Zipe Code - 5 digits only");
				return false;
			}	
			<!-- Property Phone Number -->			
			ph = document.getElementById("prop_phone").value;
			ph = ph.replace(/-/g, "");
			ph = ph.replace(/\./g, "");
			ph = ph.replace(/\(/g, "");
			ph = ph.replace(/\)/g, "");
			if (! isnumeric(ph)) {
				alert ("Invalid phone number.");
				return false;
			}	
			if (ph.length != 10) {
				alert ("Invalid phone number.");
				return false;
			}
			
			if (document.st.mfg.value == "Other"){
				if (document.st.mfgother.value == "") {
					alert("Please provide name of other manufacturer.");
					return false;
				}
			}		
			if (agency == "Pearland") {
				minin = document.st.hri.value * 60 + document.st.mini;
				if (document.st.api.value == "PM") {
					minin = minin + 720;
				}
				mino  = document.st.hro.value * 60 + document.st.mino;		
				if (document.st.apo.value == "PM") {
					mino = mino + 720;
				}
				if (minin > mino) {
					alert("Start time after end time.");
					return false;
				}
			}	
			if (agency == "Friendswood") {
				if (document.st.ihzt.checked) {
					cfok = false;
					if (document.st.feed.value == "ncf" || document.st.feed.value == "cf") {
						cfok = true;
					}
					if (! cfok) {
						alert("Please check either Chemical or Non-Chemical feed.");
						return false;
					}	
				}
			}		
			if (ac == "f") {
				document.st.action = cancpg;
				return true;
			}			
			if (ac != "") {
				document.st.action="post_form_new.php?fr=p&ac=s";
			}
			return true;
	}

Open in new window

I had to add some things to get the function to work - but I don't get any errors - what data do I need to test with

My test container
HTML
<form name="st">
	<input name="pn" type="text" value="fred">
	<input name="padr" type="text" value="fred">
	<input name="cityf" type="text" value="fred">
	<input name="zip" type="text" value="12314">
	<input name="city" type="text" value="fred">
	<input name="prop_phone" type="text" id="prop_phone" value="1234567890">
	<input name="mfg" type="text" value="fred">
</form>

Open in new window

JavaScript
var ac = 1;
var agency = 'test';

Open in new window

Julian,

In Firefox?

I'd suggest phone format 123-456-7890 (with dashes), since that fails for me.

I have Firefox Quantum, version 57.0. It 'upgraded' to this maybe 8 - 9 days ago.

Thanks,

Richard

Richard
Julian,

If you send me a private email, I can give you a logon to the "real app & you can test with the specific property that fails for me.

Thanks,

Richard
Anyone have ideas on this?

Still fails in FireFox for me.

The phone number 619-855-9192 pops the alter "Invalid Phone Number".
ASKER CERTIFIED 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
What is the error message in the Firefox console?
@Dave I think it is a validation error rather than a JavaScript one. Based on Richard's last post where he says

The phone number 619-855-9192 pops the alter "Invalid Phone Number".

I am assuming alter is a typo of alert.
alter is a typo of alert.

I just tried two tests in FireFox; one with an existing property & one with a new property.

This AM, both worked.

My FireFox is now 57.0.1.

I'm pretty sure it was 57.0 before.

Firefox console shows no error.

You guys are the experts, I can only guess it was some complex combination of my Javascript that did not work under certain conditions, no clue what those conditions were / are.
@Richard - as I mentioned in my last email I was getting an error (alert) invalid phone number when I used data that had spaces on the end.
 
Is it possible that when you were testing you had the same problem?

I am still on 57.0 and it works here so I am guessing it might have been data. If spaces cause the problem they are very easy to miss a lot of the time.
The cause & solution is unclear. I am awarding points based on effort.

Thank you both.
You are welcome.
Julian, FYI, I had suspected a trailing space. I used a "trim" to deal with that.

On my prior version, even after trim, it showed a length of 11 when the real length was 10.

Richard
Interesting - might have been another non-visible char that trim was ignoring otherwise put it down to "one of those things"

Glad you got it sorted though.
Julian,

After over 50 years of doing this kind of thing, in my experience, there is ALWAYS a logical reason why programming fails. Sometimes it is elusive, we are unable to see it.

I appreciate your strong presence on EE, I always feel relieved when I see you are pitching in with answers.

Richard