Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating a consecutive list 3

Status
Not open for further replies.

jg51

Programmer
Joined
Mar 3, 2006
Messages
3
Location
US
Hi, I have a list of variables in a form that are in a consecutive list, such as date1, date2, date3, and so on, which has the length determined from an sql query. Now, my question is, how would I validate that the fields are filled? I'm not very good at javascript, and the script I have looks like (and this is just to get the values for the variables, if I can do that I know how to validate it from there):

function checkForm(form,x)
{
for(i=1;i<=x;i++)
{
var date1="document.forms.enterNew.cDate"+i+".value";
alert(date1);
}
return false;
}

the alert gives me the actual string 'document.forms.enterNew.cDate1.value' and so on instead of what the actual value is. I have x = query.recordcount so it runs the loop as many times as there are entries, that part does work. Am I at least on the semi right track, or am I going about this completely wrong? I'm sure I left a lot that is needed out, so please pardon my newbieness.
 
Try this for size:

Code:
function checkForm(form, x) {
    for(var loop=1; loop<=x; loop++) {
    	var field = document.forms['enterNew'].elements['cDate' + loop];
    	alert('Field name: ' + field.name + '\nField value: ' + field.value);
    }
}

Incidentally, I see you're passing a "form" parameter. If this is so you can run the validation on many forms, rather than just the form names "enterNew", then this would be better:

Code:
function checkForm(theForm, x) {
    for(var loop=1; loop<=x; loop++) {
    	var field = theForm.elements['cDate' + loop];
    	alert('Field name: ' + field.name + '\nField value: ' + field.value);
    }
}


Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
use the forms and elements collections and you won't ever run into this problem:
Code:
function checkForm(form,x) {
   for(i = 1; i <= x; i++) {
      var date1 = document.[!]forms["enterNew"].elements["cDate" + i][/!].value;
      alert(date1);
   }
   return false;
}

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Don't listen to Dan or Cory, they don't even use pretty red formatting in their posts - what losers.....

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
headbang.gif




*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks guys, especially for the fast response! That was quick and painless, works great!
 
Pretty, red, and formatting. Tish and pish. Small light-coloured text on a white background and obfuscation are the way to go. If it's hard to write it should be hard to read ;-)

Code:
[small][COLOR=#cccccc]function checkForm(form, x) {
    for(var loop=1; loop<=x; loop++) {
        var field = document.forms['enterNew'].elements['cDate' + loop];
        alert('Field name: ' + field.name + '\nField value: ' + field.value);
    }
}[/color][/small]

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
[COLOR=purple blue]Purple on blue is equally appealing.[/color]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
[censored]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top