473,800 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array textbox validation using JavaScript

mageswar005
72 New Member
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 35688
nathj
938 Recognized Expert Contributor
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
Rsmastermind
93 New Member
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 Contributor
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
Sep 26 '08 #4
mageswar005
72 New Member
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 } ?>
Sep 30 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
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
72 New Member
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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator Expert
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
72 New Member
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
5084
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 valid HTML,since I've defined the function color in the javascript section of the <head> tag.
2
8108
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"; // i've tried using "\\W", "\w", "\\w","@\w", "/\\w", etc... // but it always prints my error msg UNLESS the textbox is set to "". // why does this not work?
6
27907
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 validate required format. Help me. Advance thanks.
1
2052
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 message that validation is succeseful/incorrect. I need to incorporate this 2 function together so when a user typ in the numbers it formats as a date with the / and if the date was correct dispaly check. Thsi function insert / and it working ...
2
2173
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 javascript and/or ajax. And in log in form I need to validata user id and password to my mysql database. Please guide me. Thanks & Regards, Naimesh Trivedi
6
92482
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 cities, like from Sydney, it can go to Landon, Paris and Beijing and etc. So first I use php to get the data from data base, and use a loop to list all of them and after each route, there is a input field which type is text for the customer to enter ...
1
2234
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 list, where if i choose a certian option, more fields in the form will appear. My problem is only to validate on these fields if they are visible. I think that maybe some if - else statements will do the trick, but how do i implement these....? ...
1
2155
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 contains a drop down box with selectable values from 1-9. This row is repeated 10 times with different text in each rows first column but with the exact same drop downlist in all rows...to give you a table of 10 elements long and 2 elements...
2
3451
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. str=(document.getElementById('txtEnter')).value; str.match(/()(1,))* ()/)==null (for checking < and > symbol)
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.