SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: For Loop issue

  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 ??. it checks only first value and gets stopped later.

    Here is my code

    js
    Code JavaScript:
     
    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

    Code JavaScript:
    var checkValidation1=
          NumericVal('units~units_size');
     
     
     
          if((checkValidation1==true) 
                    {
    //do updation
    }
    Can anyone why my for loop is not working?

    Thanks in advance.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
     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

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
     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

  6. #6
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply. Solved my problem. i have changed my IsNumeric function and surprisingly its working now.

    here is that function

    Code JavaScript:
    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;
     
       }

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    That's a lot of work, and it doesn't catch bad numbers like 123.456.789

    This will work.

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

    Or, even smaller:

    Code javascript:
    function isNumeric(sText) {
        return !!Number(sText);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That's a lot of work, and it doesn't catch bad numbers like 123.456.789

    This will work.

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

    Or, even smaller:

    Code javascript:
    function isNumeric(sText) {
        return !!Number(sText);
    }
    Both those fail for an empty string; the second fails for 0 entered.
    Code:
    function isNumeric( sText ) 
    {
     return /\d/.test( sText ) && !isNaN( sText );
    }
    Tab-indentation is a crime against humanity.

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Ahh yes, the wonderful case of edge-cases strikes again. Thank Logic Ali
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both the solutions are great!

    Thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •