Connecting Tech Pros Worldwide Forums | Help | Site Map

Array textbox validation using JavaScript

mageswar005's Avatar
Member
 
Join Date: Mar 2008
Location: chennai
Posts: 70
#1: Sep 26 '08
Hi Guys,

My array textbox validation is not working fine,my code is given below please help me immediately.
Expand|Select|Wrap|Line Numbers
  1. function validation()
  2. {
  3.         var chks = document.getElementsByName('rr[]');//here rr[] is the name of the textbox
  4.  
  5.         for (var i = 0; i < chks.length; i++)
  6.         {        
  7.         if (chks[i].value=="")
  8.         {
  9.         alert("Please fillup atleast one textbox");
  10.         chks[i].focus();
  11.         return false;            
  12.         }
  13.         }
  14. }

nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 857
#2: Sep 26 '08

re: Array textbox validation using JavaScript


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
Member
 
Join Date: Sep 2008
Posts: 93
#3: Sep 26 '08

re: Array textbox validation using JavaScript


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:

Expand|Select|Wrap|Line Numbers
  1. function validation() 
  2.         var chks = document.getElementsByName('rr[]');//here rr[] is the name of the textbox 
  3.         var flag=0;                     
  4.         for (var i = 0; i < chks.length; i++) 
  5.         {         
  6.             if (chks[i].value!="") 
  7.                 { 
  8.                 flag=1;
  9.                } 
  10.  
  11.  
  12.  
  13.         } 
  14.  
  15.         if (flag==0)
  16.             {
  17.                 alert("Please fillup atleast one textbox");
  18.                 return false;
  19.             }
  20.         else return true;
  21.  
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 350
#4: Sep 26 '08

re: Array textbox validation using JavaScript


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="validate()">[/HTML]
//JS code
[HTML]function validate()
{
if(document.getElementById('myText').value=="")
{
alert("Please Enter the Value");
document.getElementById('myText').focus();
}
}[/HTML]

Regards
Ramanan Kalirajan
mageswar005's Avatar
Member
 
Join Date: Mar 2008
Location: chennai
Posts: 70
#5: Sep 30 '08

re: Array textbox validation using JavaScript


Quote:

Originally Posted by RamananKalirajan

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="validate()">[/HTML]
//JS code
[HTML]function validate()
{
if(document.getElementById('myText').value=="")
{
alert("Please Enter the Value");
document.getElementById('myText').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 } ?>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Sep 30 '08

re: Array textbox validation using JavaScript


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.
mageswar005's Avatar
Member
 
Join Date: Mar 2008
Location: chennai
Posts: 70
#7: Oct 1 '08

re: Array textbox validation using JavaScript


Hi Guys,

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. function validation() 
  4. var chks = document.getElementsByName('comp_stat[]');  //here comp_stat[] is the name of the textbox 
  5.  
  6.         for (var i = 0; i < chks.length; i++) 
  7.         {         
  8.         if (chks[i].value=="") 
  9.         { 
  10.         alert("Please fillup atleast one textbox"); 
  11.         chks[i].focus(); 
  12.         return false;             
  13.         } 
  14.         } 
  15. </head>
  16. <body>
  17.  
  18. <?php for($i=0;$i<=6;$i++)    { ?>
  19.  
  20.  <input name="comp_stat[]"  type="text" class="text_box_percent"  maxlength="3" value="">
  21.  
  22. <?php    }    ?>
  23.  
  24.  
  25. <input type="submit" name="app_rej" value="Save Changes" class="button_m" onClick="return validation()" >
  26.  
  27.  
  28. </body>
  29. </html>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Oct 1 '08

re: Array textbox validation using JavaScript


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.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#9: Oct 1 '08

re: Array textbox validation using JavaScript


Quote:

Originally Posted by mageswar005

Hi Guys,

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

Expand|Select|Wrap|Line Numbers
  1. function validation()
  2. {
  3.         var chks = document.getElementsByName('rr[]');//here rr[] is the name of the textbox
  4.  
  5.         for (var i = 0; i < chks.length; i++)
  6.         {        
  7.         if (chks[i].value=="")
  8.         {
  9.         alert("Please fillup atleast one textbox");
  10.         chks[i].focus();
  11.         return false;            
  12.         }
  13.         }
  14. }

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
Expand|Select|Wrap|Line Numbers
  1. function validation()
  2. {    var allElements=document.getElementsByTagName('input');
  3.      var len=allElements.length;
  4.      for(var i =0; i < len; i++
  5.      {
  6.           var checkElement = allElements[i];
  7.           if(checkElement.type=="text")
  8.           {
  9.                /*  You know that the checkElement variable contains
  10.                     a text box element....
  11.                     here check if the text box's name contains rr
  12.                     and check if the text box contains any value
  13.                     Keep a count of the number of text boxes contains a value
  14.                     and only display the alert if this number is 0 after you finish
  15.                     looping through all of the elements.
  16.                */
  17.           }
  18.      }     
  19. }
  20.  
mageswar005's Avatar
Member
 
Join Date: Mar 2008
Location: chennai
Posts: 70
#10: Oct 15 '08

re: Array textbox validation using JavaScript


Quote:

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

Expand|Select|Wrap|Line Numbers
  1. function validation()
  2. {    var allElements=document.getElementsByTagName('input');
  3.      var len=allElements.length;
  4.      for(var i =0; i < len; i++
  5.      {
  6.           var checkElement = allElements[i];
  7.           if(checkElement.type=="text")
  8.           {
  9.                /*  You know that the checkElement variable contains
  10.                     a text box element....
  11.                     here check if the text box's name contains rr
  12.                     and check if the text box contains any value
  13.                     Keep a count of the number of text boxes contains a value
  14.                     and only display the alert if this number is 0 after you finish
  15.                     looping through all of the elements.
  16.                */
  17.           }
  18.      }     
  19. }
  20.  




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