Jump to content

for loop is not working???


deepson2

Recommended Posts

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

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/189702-for-loop-is-not-working/
Share on other sites

What does your HTML look like? It should include the following:

 

<input type="text" id="units" name="units" value="1" />
<input type="text" id="units_size" name="units_size" value="1" />

 

If that's not the problem, you'll have to describe a little better what's happening. Where is it stopping? What alerts do you get? Etc....

 

Also, try developing in Firefox and downloaded FireBug. That will tell you exactly what's going on!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.