473,396 Members | 2,111 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,396 software developers and data experts.

Validating 10 Textboxes using for loop

6
I have created ten textboxes and name them as "txt1,txt2,..txt10"
(i.e just changing suffix by consecutive number).

Now I have to validate these ten textboxes on Submit event (Say..No textbox should accept alphabet).

I trying to use for loop to validate this i.e. trying to change the suffix of textbox name in each iteration.

Expand|Select|Wrap|Line Numbers
  1. for(var i=1;i<=10;i++)
  2.  {
  3.    if(document.myform.txt+i+.value==0)
  4.     {
  5.       alert("Cannot be empty!");
  6.    }
  7.  }
But this is not working.

So please give me the suitable code for my above requirement.
Apr 29 '08 #1
7 5035
gits
5,390 Expert Mod 4TB
hi ...

first ... you have to refer correctly to the nodes:

Expand|Select|Wrap|Line Numbers
  1. for (var i = 1; i <= 10; i++) {
  2.     if(document.myform[txt + i].value == 0) {
  3.         alert("Cannot be empty!");
  4.     }
  5. }
kind regards
Apr 29 '08 #2
vee10
141 100+
hi...

and this should be there to check if it is empty or not
Expand|Select|Wrap|Line Numbers
  1. if(document.myform["txt" + i].value =="")
if defaultly u r assigning 0 then it should be 0

hi gits.....

i am getting error that it is null
at document.myform

myform is the id of the form
[HTML]<form id="myform">[/HTML]

Can u plz tell me why i am getting that





hi ...

first ... you have to refer correctly to the nodes:

Expand|Select|Wrap|Line Numbers
  1. for (var i = 1; i <= 10; i++) {
  2.     if(document.myform[txt + i].value == 0) {
  3.         alert("Cannot be empty!");
  4.     }
  5. }
kind regards
Apr 29 '08 #3
gits
5,390 Expert Mod 4TB
of course ... it must be:

Expand|Select|Wrap|Line Numbers
  1. document.myform['txt' + i].value
Apr 29 '08 #4
gits
5,390 Expert Mod 4TB
and to check for 0 should work ... just try it out:

Expand|Select|Wrap|Line Numbers
  1. var val = 0 == '';
here var val will be true ... since javascript converts the types to compare the values. in case you want to check for same types use the identity operator:

Expand|Select|Wrap|Line Numbers
  1. var val = 0 === '';
here var val will get a false assigned.

kind regards
Apr 29 '08 #5
acoder
16,027 Expert Mod 8TB
hi gits.....

i am getting error that it is null
at document.myform

myform is the id of the form
[HTML]<form id="myform">[/HTML]

Can u plz tell me why i am getting that
document.myform assumes a form named "myform", not with an id of "myform". So give your form a name "myform", or use document.getElementById("myform") to access the form.
Apr 29 '08 #6
vee10
141 100+
ok
thanks for the reply


document.myform assumes a form named "myform", not with an id of "myform". So give your form a name "myform", or use document.getElementById("myform") to access the form.
Apr 29 '08 #7
acoder
16,027 Expert Mod 8TB
ok
thanks for the reply
You're welcome.

PS. please try to bottom-post (as I have done) rather than top-post. It makes it easier to follow.
Apr 29 '08 #8

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

Similar topics

0
by: blurz | last post by:
Hi, would like to know how to go about validating a a group of textboxes. For example, I have 3 textboxes that will hold a user's different contact numbers. How do I ensure that at least one of...
6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
0
by: Bradley Bossard via DotNetMonster.com | last post by:
I am having an issue with the .NET framework (or C#) and validating events. I have implemented several validating event handlers for textboxes on a form. When I run the app, the form works...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
7
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
6
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call...
0
by: Rolf | last post by:
I create several textboxes in code on the fly, using the same function. I attach a validating event in that function that calls a validating routine.. but somehow on some textboxes the validating...
2
by: lenin42001 | last post by:
Help i've just done a basic calculator with three textboxes a label & a button, basically when you add a number in textbox1 to the one in textbox2 it appears in textbox3 .........now the question...
8
by: clintonG | last post by:
I have to get to this later tonight or tommorrow...and wonder... There's two TextBoxes in a Wizard Step posing an either-or situtation. Only one or the other TextBox may pass data. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.