473,888 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Script to check n number of values in n number of rows

36 New Member
Hi,

I'm facing a pretty peculiar problem.

In one of my pages I print the number of rows depending upon the number of Qty I got filled in my previous page i.e. If Qty is filled in as 3 in previous page 3 rows would be printed on my current page or, if 5 being entered in the previous page 5 rows would get printed in my current page & so on.

Now the problem is, I want to check whether the required fields in each row has been filled or, not. If not ONSUBMIT the page should display an alert msg stating the specified field has to be filled.

Expand|Select|Wrap|Line Numbers
  1.  
  2. function checkfields(f1) 
  3.    i = 0 
  4.    while (i <= <?php echo $Qty; ?>) 
  5.    { 
  6.         if(f1.inventory+i.value==0) 
  7.         { 
  8.                 alert("Please enter the Inventory Item"); 
  9.                 f1.inventory+i.focus(); 
  10.                 return false; 
  11.         } 
  12.    } 
  13. </script>
  14.  
This is the Javascript function I tried using but after certain time I get an alert stating The script is taking too long. Continuing this script may make the system unresponsive... ...The problem remains same both with IE7 & Firefox v2.0.0.3. I'm using this javascript function in a PHP page.

I guess this is because of this line "f1.inventory+i .value==0" i.e. its not filling in "inventory+ i" as inventory1, inventory2, inventory3 & so on.

Kindly help me with a function that could help me check my n number of rows for n number of fields.
May 10 '07 #1
14 1908
gits
5,390 Recognized Expert Moderator Expert
hi,

in javascript you cannot use the '.' reference that way - try to refer with:

f1['inventory' + i].value

instead. this should work for your purpose until i is the only variable that you want to add to your stringobject 'inventory'? otherwise we need more information about the 'datastructure' you want to check.

greetings ... hope this helps ;)
May 10 '07 #2
gits
5,390 Recognized Expert Moderator Expert
and wait a moment ;)

the solution above is of course not very slick have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. var foo = {
  2.     bar: 1,
  3.     bar1: 5,
  4.     bar2: 9
  5. };
  6.  
  7. // running through the objects-properties
  8.  
  9. for (var i in foo) {
  10.     alert(foo[i]);
  11. }
that gives you the values of all of foos properties ... an is the easier (perhaps the common) way to refer to an objects properties. try to adopt that to your purpose ;)

grettings again
May 10 '07 #3
ankitmathur
36 New Member
Hi,

Thanks for your inputs.

Am so sorry I couldn't reply earlier to your post.

Well....to update you on the latest on this problem I was able to solve one issue of checking all blank textboxes on my page.

While working on the same module a new condition has arise which is again giving me some headache.

Of the whole form two of my fields are as such that my form may get submitted even if any one of them is empty. However, if both the fields are empty my function should return back an alert stating "Any one of the two fields have to be filled".

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. function checkfields(f1)
  3. {
  4.   var inputs = f1.getElementsByTagName("input");
  5.   for(var i=0; i < inputs.length; i++)
  6.   {
  7.       if(inputs[i].type == "text" && inputs[i].className == 'required')
  8.       {
  9.           if(inputs[i].value=='')
  10.           {
  11.                   alert("Please enter the Inventory Item");
  12.                   inputs[i].focus();
  13.                   return false;
  14.           }
  15.       }
  16.   }
  17. }
  18. </script>
  19.  
This particular function is working perfectly fine but it checks for all fields to be filled in. My new condition requires it to OK page if any of the two fields are filled.

My input tags look like this

Expand|Select|Wrap|Line Numbers
  1. <form name="f1" method="GET" action="invsql.php" onsubmit="return checkfields(this)">
  2.  ....
  3.     <td><input type="text" class="required" name="MAC<? echo $i; ?>" Id="MAC"  /></td>
  4.     <td><input type="text" class="required" name="SerialNo<? echo $i; ?>" Id="SerialNo" /></td>
  5. ....
  6. </form>
  7.  
This is one of the things I tried

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. function checkfields(f1)
  3. {
  4.   var inputs = f1.getElementsByTagName("input");
  5.   for(var i=0; i < inputs.length; i++)
  6.   {
  7.       if(inputs[i].type == "text" && inputs[i].className == 'required')
  8.       {
  9.           if(document.getElementById('MAC').value=='' && document.getElementById('SerialNo').value=='')
  10.           {
  11.                   alert("Please enter the Inventory Item");
  12.                   inputs[i].focus();
  13.                   return false;
  14.           }
  15.       }
  16.   }
  17. }
  18. </script>
  19.  
However, contrary to my expectation that it'll check each row & hence would check MAC & SerialNo in each row by its Id it just checks for validation in first row & moves ahead even if rest all MAC & SerialNos remain empty.

As again .. I'm working on Php with Mysql & problem is valid for Both IE & FireFox.

I think the problem is I'm not able to somehow make it check each row at a time though being in a loop it should be doing that.

I would appreciate if anybody could help.
May 17 '07 #4
leela mn
43 New Member
chk the modified code...
let me know if it solves u r problem.......

<script language="JavaS cript" type="text/javascript">
function checkfields(f1)
{
var inputs = f1.getElementsB yTagName("input ");
for(var i=0; i < inputs.length; i= i+2)
{
if(inputs[i].type == "text" && inputs[i].className == 'required' && inputs[i+1].type == "text" && inputs[i+1].className == 'required')
{
if(inputs[i].value=='' && inputs[i+1] value == "")
{
alert("Please enter any one value");
inputs[i].focus();
return false;
}
}
}
}
</script>
May 17 '07 #5
ankitmathur
36 New Member
Hi Leela,

Thanks for contributing.
I just tried out your code and It worked like a charm.

Its working absolutely as I needed.

Thanks a lot.....

It's been a very long standing problem which got resolved just now.

All Thanks to you.

However, I need one more favour from you.

Can you guide me through your code. What exactly is happening at what line. This is just to gain some crucial information on where I was failing & what I could do in certain similar situations in future.

Thanks again for solving my problem....
Ankit Mathur


chk the modified code...
let me know if it solves u r problem.......

<script language="JavaS cript" type="text/javascript">
function checkfields(f1)
{
var inputs = f1.getElementsB yTagName("input ");
for(var i=0; i < inputs.length; i= i+2)
{
if(inputs[i].type == "text" && inputs[i].className == 'required' && inputs[i+1].type == "text" && inputs[i+1].className == 'required')
{
if(inputs[i].value=='' && inputs[i+1].value == "")
{
alert("Please enter any one value");
inputs[i].focus();
return false;
}
}
}
}
</script>
May 18 '07 #6
leela mn
43 New Member
hey thanks for u r appreciation :)
go through ths......
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. function checkfields(f1)
  3. {
  4. //retrieves all the elemts whose tagname ia INPUT
  5. var inputs = f1.getElementsByTagName("input");
  6. //loop through the elements incrementing two at one timw
  7. for(var i=0; i < inputs.length; i= i+2)
  8. {
  9. //compare the first element i.e., (i)th element with the (i+1)th element
  10. if(inputs[i].type == "text" && inputs[i].className == 'required' && inputs[i+1].type == "text" && inputs[i+1].className == 'required')
  11. {
  12. //if both the values are null
  13. if(inputs[i].value=='' && inputs[i+1] value == "")
  14. {
  15. //display the error msg
  16. alert("Please enter any one value");
  17. inputs[i].focus();
  18. //stop the processing and return
  19. return false;
  20. }
  21. }
  22. }
  23. }
  24.  
May 18 '07 #7
ankitmathur
36 New Member
Hi Leela,

Thanks for explaning the function. However, I have one more doubt.

Why did u used i+2 in for loop?

Ankit


hey thanks for u r appreciation :)
go through ths......
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. function checkfields(f1)
  3. {
  4. //retrieves all the elemts whose tagname ia INPUT
  5. var inputs = f1.getElementsByTagName("input");
  6. //loop through the elements incrementing two at one timw
  7. for(var i=0; i < inputs.length; i= i+2)
  8. {
  9. //compare the first element i.e., (i)th element with the (i+1)th element
  10. if(inputs[i].type == "text" && inputs[i].className == 'required' && inputs[i+1].type == "text" && inputs[i+1].className == 'required')
  11. {
  12. //if both the values are null
  13. if(inputs[i].value=='' && inputs[i+1] value == "")
  14. {
  15. //display the error msg
  16. alert("Please enter any one value");
  17. inputs[i].focus();
  18. //stop the processing and return
  19. return false;
  20. }
  21. }
  22. }
  23. }
  24.  
May 19 '07 #8
gits
5,390 Recognized Expert Moderator Expert
hi ... cool ... you've got your problems solved with this ... your last question was about the i+2 incrementation ... and i think this is used due to the fact ... that within the loop always 2 input-elements will be compared ... and thats a drawback of this solution ... you MUST have 2 'required' textboxes right as neighbors (and you have to know implicitly, that they may be 'complementary' ) ... in the case of putting one 'unrequired' input-element between them the check will fail ... so you have to know your dom-structure and to use this check you have to have that in mind while constructing your forms ... with that it will do the job well :)

if you need a more generic check perhaps due to a more flexible form-design you have to adapt that code ... if you need that ... post a question here
May 20 '07 #9
ankitmathur
36 New Member
Hi Gits,

Thanks for explaining. Now I have understood the code well.

You're right, I tried inserting a new input that wasn't "class=required " in between my required input boxes & the check got into some sort of trouble.

Seems like we still have a problem to discuss further.

Ankit

hi ... cool ... you've got your problems solved with this ... your last question was about the i+2 incrementation ... and i think this is used due to the fact ... that within the loop always 2 input-elements will be compared ... and thats a drawback of this solution ... you MUST have 2 'required' textboxes right as neighbors (and you have to know implicitly, that they may be 'complementary' ) ... in the case of putting one 'unrequired' input-element between them the check will fail ... so you have to know your dom-structure and to use this check you have to have that in mind while constructing your forms ... with that it will do the job well :)

if you need a more generic check perhaps due to a more flexible form-design you have to adapt that code ... if you need that ... post a question here
May 22 '07 #10

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

Similar topics

0
1378
by: Mario T. Lanza | last post by:
Seasoned ASP Developers, I have developed an ASP page that displays multiple rows of data with which the user may work. As each row is updated, the graphical info displayed immediately beside the row reflects the changes. Fortunately, the script (although a little intensive and slow) runs quickly enough (about 1 sec.) for updates against a single row. The issue I am running into is that the client-side script must be run for each and...
5
2969
by: Yellowbird | last post by:
Hi all, I'm new to JavaScript, but am pretty sure what I want to accomplish is not that difficult. I just need an example or suggestion to help clarify it for me - I haven't had much time to learn JS, so I have been using some prebuilt scripts where needed. I have a Web page form where a user can enter data. The first step is for them to select a dropdown menu to indicate the number of entries they will be making (the dropdown menu...
1
5967
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the SET INTEGRITY statement C. With the DELETE phase of the LOAD D. With the UPDATE CONSTRAINTS statement Answer is A
7
43151
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is how the data is at the moment: Company_ID Material 0x00C00000000053B86 Lead 0x00C00000000053B86 Sulphur 0x00C00000000053B86 Concrete
3
3690
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of html is: <a class="searchsavechanges btn btn3d tbbtn" href="javascript:" style="position:static"> <div id="TBsearchsavechanges">Search</div> </a>
9
2408
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
3
1786
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
15
3267
by: Lawrence Krubner | last post by:
Does anything about this script look expensive, in terms of resources or execution time? This script dies after processing about 20 or 25 numbers, yet it leaves no errors in the error logs. This is on a server that handles a fairly demanding site. The defaults, in php.ini, have all been cranked fairly high: scripts get 180 seconds to run, and they can have as much as 256 megs of RAM. The input for this script is coming from a textarea in...
1
47519
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9800
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
11176
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...
1
10880
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
10434
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
7988
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
7144
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5812
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
4638
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
3
3246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.