I want to check my values are numeric or not.I have been loosing my patients for why my for loop is not working ??. it checks only first value and gets stopped later.
Here is my code
js
function NumericVal(fieldList1)
{
var field1=new Array();
field1=fieldList1.split("~");
//alert(field1);
var counter=0;
for(i=0;i<field1.length;i++) {
//alert(document.getElementById(field1[i]).value);
if((document.getElementById(field1[i]).value !="") && ( IsNumeric(document.getElementById(field1[i]).value)==false))
{
document.getElementById(field1[i]).style.backgroundColor="#FF0000";
counter++;
}
}
//i++;
if(counter > 0)
{
alert("Please enter only numeric values");
return false;
}
else {
return true;
}
}
function IsNumeric(strString)
// check for valid numeric strings
{
var strValidChars = "0123456789.-";
var iChars = "`~=!@#$%^&*()<>+_[]{}\\\\;:\\"\\'?\\/";
var strChar;
var blnResult = true;
if (strString.length == 0) return false;
// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
//alert("Please enter only numeric values");
}
}
return blnResult;
}
then calling it here
var checkValidation1=
NumericVal('units~units_size');
if((checkValidation1==true)
{
//do updation
}
Thanks for your dan, I know because i am putting alert in the for loop
when my unit value is “10” or i have put up wrong value something like “abc” it shows me the proper alert but when i put something wrong in unite_size’s value , it wont work only? it means it works for only first time. isnt it?
These field are very much exist. In Isnumeric function there is a for loop and again i am checking it in NumericVal functions as well. Do you think that could be the cause my main for loop is not working?
I mean you have no check that “units” and “units_size” are objects in the document. Are you assured that whatever string you pass in will consist of only real fields?
Or we can skip the Q/A and you can just run Firebug in Firefox or the JavaScript debugger in Chrome. Step through the loop and find out when an error occurs or when it leaves and why.
Sorry for the late reply. Solved my problem. i have changed my IsNumeric function and surprisingly its working now.
here is that function
function IsNumeric(sText)
{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}