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>

Recommended Answers

All 2 Replies

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"

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.