By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Array textbox validation using JavaScript

mageswar005
P: 72
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. }
Sep 26 '08 #1
Share this Question
Share on Google+
9 Replies


nathj
Expert 100+
P: 938
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
Sep 26 '08 #2

P: 93
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.  
Sep 26 '08 #3

RamananKalirajan
100+
P: 607
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
Sep 26 '08 #4

mageswar005
P: 72
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 } ?>
Sep 30 '08 #5

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 30 '08 #6

mageswar005
P: 72
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>
Oct 1 '08 #7

acoder
Expert Mod 15k+
P: 16,027
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.
Oct 1 '08 #8

Frinavale
Expert Mod 5K+
P: 9,731
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.  
Oct 1 '08 #9

mageswar005
P: 72
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 ...
Oct 15 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.