Array textbox validation using JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mageswar005
    New Member
    • Mar 2008
    • 72

    Array textbox validation using JavaScript

    Hi Guys,

    My array textbox validation is not working fine,my code is given below please help me immediately.
    [code=js]
    function validation()
    {
    var chks = document.getEle mentsByName('rr[]');//here rr[] is the name of the textbox

    for (var i = 0; i < chks.length; i++)
    {
    if (chks[i].value=="")
    {
    alert("Please fillup atleast one textbox");
    chks[i].focus();
    return false;
    }
    }
    }[/code]
    Last edited by pbmods; Sep 26 '08, 10:58 AM. Reason: Added CODE tags.
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Okay, first this seems to be a javascript issue not a php issue so I've reported it so that it can be moved to the correct forum - you;ll get better help that way,

    Second, you need to answer the following questions:

    1. What is the code doing that it should not do?
    2. What is the code not doing that it should do?
    3. Are you getting any error message? - If so please give them.

    I assume this JS function is called from some sort of onchange event handler on the text box.

    Cheers
    nathj

    Comment

    • Rsmastermind
      New Member
      • Sep 2008
      • 93

      #3
      Please friend post the Html code on which you are operating the code.

      As I can observe from your code is that you have sets of text fields.And there is button
      if the user has not enter any of the field then on pressing that button it should give alert
      to fill at least one .

      So this could be the solution to your code:

      Code:
      function validation() 
      { 
              var chks = document.getElementsByName('rr[]');//here rr[] is the name of the textbox 
              var flag=0;                     
              for (var i = 0; i < chks.length; i++) 
              {         
              	if (chks[i].value!="") 
             		 { 
              		flag=1;
            		 } 
      
      
      		
              } 
      
      		if (flag==0)
      			{
      				alert("Please fillup atleast one textbox");
      				return false;
      			}
      		else return true;
      			
      }

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Can u please tell me what is the content of the textbox "rr[ ]" wether rr[ ] is the name of a single textbox or are u having many text box with the name "rr".
        If you are having a single text box means, here is your answer. Use Id instead of name.

        [HTML]//HTML code
        <input type="text" id="myText" onblur="validat e()">[/HTML]
        //JS code
        [HTML]function validate()
        {
        if(document.get ElementById('my Text').value==" ")
        {
        alert("Please Enter the Value");
        document.getEle mentById('myTex t').focus();
        }
        }[/HTML]

        Regards
        Ramanan Kalirajan

        Comment

        • mageswar005
          New Member
          • Mar 2008
          • 72

          #5
          Originally posted by RamananKaliraja n
          Can u please tell me what is the content of the textbox "rr[ ]" wether rr[ ] is the name of a single textbox or are u having many text box with the name "rr".
          If you are having a single text box means, here is your answer. Use Id instead of name.

          [HTML]//HTML code
          <input type="text" id="myText" onblur="validat e()">[/HTML]
          //JS code
          [HTML]function validate()
          {
          if(document.get ElementById('my Text').value==" ")
          {
          alert("Please Enter the Value");
          document.getEle mentById('myTex t').focus();
          }
          }[/HTML]

          Regards
          Ramanan Kalirajan

          Hi,

          I Have many text box with the name "rr".
          <?php for($i=0;$i<10; $i++) { ?>
          <input type="textbox" name="rr[]" >
          <?php } ?>

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            It's not enough just to say it's not working. That could mean absolutely anything.

            Having said that, I think the solution provided by Rsmastermind should work for you.

            Comment

            • mageswar005
              New Member
              • Mar 2008
              • 72

              #7
              Array textbox validation using javascript

              Hi Guys,

              My array textbox validation is not working fine,my code is given below please help me immediately.

              Code:
              <html>
              <head>
              function validation() 
              { 
              var chks = document.getElementsByName('comp_stat[]');  //here comp_stat[] is the name of the textbox 
                
                      for (var i = 0; i < chks.length; i++) 
                      {         
                      if (chks[i].value=="") 
                      { 
                      alert("Please fillup atleast one textbox"); 
                      chks[i].focus(); 
                      return false;             
                      } 
                      } 
              } 
              </head>
              <body>
              
              <?php for($i=0;$i<=6;$i++)    { ?>
              
               <input name="comp_stat[]"  type="text" class="text_box_percent"  maxlength="3" value="">
              
              <?php    }    ?>
              
              
              <input type="submit" name="app_rej" value="Save Changes" class="button_m" onClick="return validation()" >
              
              
              </body>
              </html>
              Last edited by acoder; Oct 1 '08, 11:12 AM. Reason: Added [code] tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                mageswar005, as a full member now, you should know that we expect your code to be posted in [CODE] tags (See How to Ask a Question).

                This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                Please use the tags in future.

                In addition to this, please do not double post your questions.

                Moderator.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by mageswar005
                  Hi Guys,

                  My array textbox validation is not working fine,my code is given below please help me immediately.
                  [code=js]
                  function validation()
                  {
                  var chks = document.getEle mentsByName('rr[]');//here rr[] is the name of the textbox

                  for (var i = 0; i < chks.length; i++)
                  {
                  if (chks[i].value=="")
                  {
                  alert("Please fillup atleast one textbox");
                  chks[i].focus();
                  return false;
                  }
                  }
                  }[/code]
                  You don't need to Focus on an element to check if it has content.
                  You can retrieve all of the Input elements on your page, loop through all of the elements check if the type is a text box...if the text box's name contains "rr"...and if that text box contains anything as it's value.

                  For example
                  [code=javascript]
                  function validation()
                  { var allElements=doc ument.getElemen tsByTagName('in put');
                  var len=allElements .length;
                  for(var i =0; i < len; i++
                  {
                  var checkElement = allElements[i];
                  if(checkElement .type=="text")
                  {
                  /* You know that the checkElement variable contains
                  a text box element....
                  here check if the text box's name contains rr
                  and check if the text box contains any value
                  Keep a count of the number of text boxes contains a value
                  and only display the alert if this number is 0 after you finish
                  looping through all of the elements.
                  */
                  }
                  }
                  }
                  [/code]

                  Comment

                  • mageswar005
                    New Member
                    • Mar 2008
                    • 72

                    #10
                    Originally posted by Frinavale
                    You don't need to Focus on an element to check if it has content.
                    You can retrieve all of the Input elements on your page, loop through all of the elements check if the type is a text box...if the text box's name contains "rr"...and if that text box contains anything as it's value.

                    For example
                    [code=javascript]
                    function validation()
                    { var allElements=doc ument.getElemen tsByTagName('in put');
                    var len=allElements .length;
                    for(var i =0; i < len; i++
                    {
                    var checkElement = allElements[i];
                    if(checkElement .type=="text")
                    {
                    /* You know that the checkElement variable contains
                    a text box element....
                    here check if the text box's name contains rr
                    and check if the text box contains any value
                    Keep a count of the number of text boxes contains a value
                    and only display the alert if this number is 0 after you finish
                    looping through all of the elements.
                    */
                    }
                    }
                    }
                    [/code]



                    thanks guys , here after i will use tag ...

                    Comment

                    Working...