For Loop issue

I want to check my values are numeric or not.I have been loosing my patients for why my for loop is not working ??. :nono: 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
}

Can anyone why my for loop is not working?:frowning:

Thanks in advance.

How do you know it’s not running more than once? Have you stepped through it with a debugger? That’ll probably tell ya what’s up.

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?

 document.getElementById(field1[i]).value

If these fields may not exist then you should check their existence before trying to call .value on a non-object

 document.getElementById(field1[i]).value

If these fields may not exist then you should check their existence before trying to call .value on a non-object

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. :slight_smile:

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;
   
   }

That’s a lot of work, and it doesn’t catch bad numbers like 123.456.789

This will work.


function isNumeric(sText) {
    return !isNaN(Number(sText));
}

Or, even smaller:


function isNumeric(sText) {
    return !!Number(sText);
}

Both those fail for an empty string; the second fails for 0 entered.


function isNumeric( sText ) 
{
 return /\\d/.test( sText ) && !isNaN( sText );
}

Ahh yes, the wonderful case of edge-cases strikes again. Thank Logic Ali

Both the solutions are great!

Thank you! :slight_smile: