0

Hi im zee and i'm new to javascript..
The javascript function below..examines if a string value contains only numbers..
My problem is..I can't figure out why it doesn't continue with the 2nd loop and iterate j..please help.!

<script language="javascript">
<!--
	value = "23e";
	isNumber(value);
	
	function isNumber(value){
		document.write('I\'m inside the function isNumber' + '</br>');
		document.write('The value = ' + value + '</br>' );
		
		document.write('The length of value = ' + value.length);
		var isAllValid=true;						document.write(isAllValid);
		var validChars="0123456789";		document.write('</br>' + 'validChars = ' + validChars + '</br>');
		var current = "";
		
		for(i=0; i<value.length; i++){
			current = value.charAt(i);  
			document.write('current = ' + current + '</br>');
			
				for(j=0; j<validChars.length; j++){	
					document.write('j = ' + j + '</br>');
					document.write('current = ' + current + '  ' + 'validChars = ' + validChars.charAt(j) + '</br>');
					if(current == validChars.charAt(j)){
							document.write('break!');
							break;
					}
					if(j==validChar.length){
							document.write('oops! 1-9 evaluated...no match');
							isAllValid=false;
							break;
					}
				}
				
		}
		return isAllValid;
	}
	
-->
</script>

Edited by poison_bubbles: wrong spelling

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by james chaco
0

Could you not just use the match method?

var str="A string";
if(str.match(/^[0-9]+$/)){
   alert("Only Numbers");
} else{
   alert("Not only numbers");
}
// Would alert "Not only numbers"
0

Hi im zee and i'm new to javascript..
The javascript function below..examines if a string value contains only numbers..
My problem is..I can't figure out why it doesn't continue with the 2nd loop and iterate j..please help.!

<script language="javascript">
<!--
	value = "23e";
	isNumber(value);
	
	function isNumber(value){
		document.write('I\'m inside the function isNumber' + '</br>');
		document.write('The value = ' + value + '</br>' );
		
		document.write('The length of value = ' + value.length);
		var isAllValid=true;						document.write(isAllValid);
		var validChars="0123456789";		document.write('</br>' + 'validChars = ' + validChars + '</br>');
		var current = "";
		
		for(i=0; i<value.length; i++){
			current = value.charAt(i);  
			document.write('current = ' + current + '</br>');
			
				for(j=0; j<validChars.length; j++){	
					document.write('j = ' + j + '</br>');
					document.write('current = ' + current + '  ' + 'validChars = ' + validChars.charAt(j) + '</br>');
					if(current == validChars.charAt(j)){
							document.write('break!');
							break;
					}
					if(j==validChar.length){
							document.write('oops! 1-9 evaluated...no match');
							isAllValid=false;
							break;
					}
				}
				
		}
		return isAllValid;
	}
	
-->
</script>

There are 2 problems in this code.
1). line=26, "if(j==validChar.length)" you have variable name as validChars and int this line you used wrongly.

2). line=26, value of j and value of validChars.length will never match.


to optimize this code u can use parse() method.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.