473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Array textbox validation using JavaScript

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. }
Sep 26 '08 #1
9 35621
nathj
938 Expert 512MB
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
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
608 512MB
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
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

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

Similar topics

2
by: trusst_3 | last post by:
Hello, This is the only line in my entire code that does not validate in XHTML : <td width="24" bgcolor="#330066" onclick="javascript:color1()" class="borderWidthr"></td> However, it is...
2
by: live your lives | last post by:
i am trying to validate a simple username textbox using RegularExpressionValidator: TextBox tbUserName = new TextBox(); tbUserName.ID = "tbUserName"; string strPatternUserName = @"\W"; //...
6
by: prabhunew2005 | last post by:
Hi all, I need to allow the date format only yyyy-mm-dd to store into mysql4 data base. I have the validation coding for mm-dd-yyyy format. But i could not modify to convert it to...
1
by: etsomik | last post by:
Hi guys. I need some help in javascript. I wrote a function which supposed to validate the date. And it does working it addin slashes as you type that what I want, However now I need to display a...
2
by: nbt725 | last post by:
Dear Sir, Hello ! I need to validate my login form which is displayed using <div> to give sliding effect and not to refresh page, hence can't use generic php submit but to validate using...
6
by: lucoin | last post by:
Hello guys, I met a problem about php and javascipt For a tickets booking system, when a customer search for flights from one city, so in the data base there should be many routes to different...
1
by: printline | last post by:
Hello All I'm a newbee to javascript/ajax. I have produced a form, where i want to do some validation on some fields. I have used the spry framework and it works fine. Now, i have a select...
1
by: scott | last post by:
Hello, Thanks in advance for any help you might be able to offer... I have an html table with two columns and 10 rows. The first column contains a textbox that is populated. The second column...
2
by: mathewgk80 | last post by:
Hi, I am trying to check whether the textbox contains single quote,double quote and < and > symbols. I got the regex to check all the requirements. its as shown below. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.