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).
Richard KortsAsked:
Who is Participating?
 
Julian HansenCommented:
@Richard,
Apologies for not replying to your earlier post - I missed the notification.

I tried both 619-855-9192 and 123-456-7890 running on Quantum 57 - does not give me an invalid phone number.

Sidebar: The first time I tried the second one my cut and paste added a trailing space to the number and that did result in an invalid phone number alert - so maybe check for the trailing space.

Here is my code
<!-- SOURCE LISTING ACTIVE -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
<title>Title</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="css/custom.css" rel="stylesheet" />
<style type="text/css">
</style>
</head>
<body>
<div class="wrapper">
  <header>
    <div class="container">
    <h1 class="col-lg-9">Title</h1>
    
    </div>
  </header>
  <div class="container">
    <h5>Author: Julian Hansen, November 2017</h5>
	<p class="alert alert-success"></p>
<!-- START "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="619-855-9192">
	<input name="mfg" type="text" value="fred">
</form>
<!-- END -->
    <script src="js/psource.js"></script>
  </div>
</div>
<footer>
  <div class="container">
    Copyright Julian Hansen &copy; 2017
  </div>
</footer>

<!-- START "jQuery" -->
<script>
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;
}
var ac = 1;
var agency = 'test';
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;
}
chk_vals();
</script>
<!-- END -->
<script src="js/ga.js"></script>
</body>
</html>

Open in new window

Online version here
0
 
Richard KortsAuthor Commented:
Must be Firefox; I tried in Chrome, worked fine.
0
 
Dave BaldwinFixer of ProblemsCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dave BaldwinFixer of ProblemsCommented:
In Firefox, you can press Ctl-Shift-J to bring up the error console and see what Firefox doesn't like.
0
 
Richard KortsAuthor Commented:
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
0
 
Dave BaldwinFixer of ProblemsCommented:
If you read what I said and posted, you will see that those functions aren't the problem.  The second piece of code is the problem.  'return' is not valid in an 'if' statement.  Never has been no matter whether it works at the moment in Chrome.  You can't count on an invalid bit of code continuing to work in the future.
0
 
Richard KortsAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
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'.
0
 
Julian HansenCommented:
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.
0
 
Julian HansenCommented:
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

0
 
Richard KortsAuthor Commented:
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

0
 
Julian HansenCommented:
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

0
 
Richard KortsAuthor Commented:
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
0
 
Richard KortsAuthor Commented:
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
0
 
Richard KortsAuthor Commented:
Anyone have ideas on this?

Still fails in FireFox for me.

The phone number 619-855-9192 pops the alter "Invalid Phone Number".
0
 
Dave BaldwinFixer of ProblemsCommented:
What is the error message in the Firefox console?
0
 
Julian HansenCommented:
@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.
0
 
Richard KortsAuthor Commented:
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.
0
 
Julian HansenCommented:
@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.
0
 
Richard KortsAuthor Commented:
The cause & solution is unclear. I am awarding points based on effort.

Thank you both.
0
 
Julian HansenCommented:
You are welcome.
0
 
Richard KortsAuthor Commented:
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
0
 
Julian HansenCommented:
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.
0
 
Richard KortsAuthor Commented:
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
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.