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

Validating 10 Textboxes using for loop

P: 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
Share this Question
Share on Google+
7 Replies


gits
Expert Mod 5K+
P: 5,390
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

100+
P: 141
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
Expert Mod 5K+
P: 5,390
of course ... it must be:

Expand|Select|Wrap|Line Numbers
  1. document.myform['txt' + i].value
Apr 29 '08 #4

gits
Expert Mod 5K+
P: 5,390
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
Expert Mod 15k+
P: 16,027
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

100+
P: 141
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
Expert Mod 15k+
P: 16,027
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

Post your reply

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