How to find if argument is numeric or not, using Javascript

Use the code below:

// checks to see whether the input is a numeric string or not also includes "." and "-"
function IsNumeric(strString)
{
	var strValidChars = "0123456789.-"; // valid characters for a numeric string
	var strChar;
	var result = true;
	var i = 0
	if (strString.length == 0) return false;
	// check to see if strString consists of valid characters listed in strValidChars
	while (i < strString.length && result == true)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) return false;
		i++;
	}
	return result;
}

Please feel free to use the comments form below if you have any questions or need more explanation on anything. Use the icons below to share this tutorial with your friends.

tags cloud

javascript

popular searches