function getNum(str) { var num = 0; for (var i = 0; i < str.length; i++) { var char = str.charAt(i); if (char != " " && !isNaN(char)) { num++; } } return num; } document.write(getNum("12sdf983jkdsa23"));