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

asked on

Javascript Error

See this code.

<html>
<head>
<title>Untitled</title>
<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;
	}
	function val_phone() {
		alert("Entered val_phone");
		ph = document.getElementById("phone");
		alert("ph = " + ph);
		ph = ph.replace(/-/g, "");
		ph = ph.replace(/./g, "");
		ph = ph.replace(/(/g, "");
		ph = ph.replace(/)/g, "");
		if (! isnumeric(ph)) {
			alert ("Invalid phone number.");
		}	
		if (ph.length != 10) {
			alert ("Invalid phone number.");
		}	
		alert("Phone OK!!");
		return false;
	}	
</script>
</head>
<body>
Phone:&nbsp;<input type="text" id="phone">&nbsp; <button onclick="val_phone();">Validate</button> 



</body>
</html>

Open in new window


When I run it, it does NOTHING.

Note the alerts at the top of the function. They NEVER display.

Can someone tell me what is wrong?

Thanks
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

I ran it and I got an alert. Which browser you are using?
Also how about putting debugger; at the beginning of your code and try debugging?
Avatar of Richard Korts

ASKER

I'm using FireFox. I used FireBug, it found NOTHING.

This is CRAZY.

Please explain what you mean by "putting debugger". I don;t know what that means, is than something in Chrome?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of John Easton
John Easton
Flag of United Kingdom of Great Britain and Northern Ireland 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
I am sorry I am posting from a mobile device hence keeping things short.
debugger; is a special statement that instructs browser to enable debugging. You can use them to inspect how your code is actually executing to when it runs.
I am sorry I am posting from a mobile device hence keeping things short.
debugger; is a special statement that instructs browser to enable debugging. You can use them to inspect how your code is actually executing to when it runs.
I modified the code like this. Note all the alerts.

Note also I DID NOT have .value after the getElementById before.

The first 3 work (in Chrome).

The 4th one, after replace . gives a null string.

So must be something wrong with that. I'm also suspicious of the ( & )

<!DOCTYPE html>

<html>
<head>
<title>Untitled</title>
<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;
	}
	function val_phone() {
		alert("Entered val_phone");
		ph = document.getElementById("phone").value;
		alert("ph = " + ph);
		ph = ph.replace(/-/g, "");
		alert("ph after repl dashes = " + ph);
		ph = ph.replace(/./g, "");
		alert("ph after repl . = " + ph);
		ph = ph.replace(/(/g, "");
		alert("ph after repl ( = " + ph);
		ph = ph.replace(/)/g, "");
		alert("ph after repl ) = " + ph);
		if (! isnumeric(ph)) {
			alert ("Invalid phone number.");
			return false;
		}	
		if (ph.length != 10) {
			alert ("Invalid phone number.");
			return false;
		}	
		alert("Phone OK!!");
		return false;
	}	
</script>
</head>
<body>
Phone:&nbsp;<input type="text" id="phone">&nbsp; <button onclick="val_phone();">Validate</button> 



</body>
</html>

Open in new window

To John Easton,

Also maybe that same structure for .?

See my just last comment.

Thanks
There are two major issues in your code in val_phone() function only:

1. You need to escape the parenthesis () in the regular expressions \(\)
2. You are using ph.replace replace is a method which we can use to replace from a string, not an Object, and you are using this with an Object ph is an input element not the value of that element.

Try the updated code for val_phone() function:

function val_phone() {
  console.log("Entered val_phone");
  ph = document.getElementById("phone");
  console.log("ph = ", ph);
	var phoneNumber = ph.value;
  phoneNumber = phoneNumber.replace(/-/g, "").replace(/./g, "").replace(/\(/g, "").replace(/\)/g, "");
  if (!isnumeric(phoneNumber)) {
    alert("Invalid phone number.");
  }
  if (phoneNumber.length != 10) {
    alert("Enter 10 digit phone number.");
  }
  return false;
}

Open in new window

Did same for . (dot).

Works now in Chrome & Firefox.

Thanks!