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
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.