I have the following code below, but i don't understand this part for (var s_i=0;s_i<s_len;s_i++) can anyone help to use different variables or provide new function script. I need javascript function to validate a text field on a form which is to contain an integer, without using isNaN. function checkNum(id) { var x = document.getElementById(id); var s_len=x.value.length ; var s_charcode = 0; for (var s_i=0;s_i<s_len;s_i++) { s_charcode = x.value.charCodeAt(s_i); if(!((s_charcode>=48 && s_charcode<=57))) { alert("Only Numeric Values Allowed"); x.value=''; x.focus(); return false; } } return true; } </script>
if you only need numerics then do: var myval = parseInt(document.getElementById("blah").value, 10); // strips non numerics. Code (javascript): the code you are asking about is a simple loop that runs from 0 until the length of the string of the value, checking if each char at the offset is within the allowed range for numerics. i'd probably use regex for this even if it's slower