|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi!
I have written a dynamic form, which is used to add entries to a database. Typically, the form contains fields like: HistoryDate_1 HistoryDate_2 HistoryComment_1 HistoryComment_2 etc I want to validate some of these fields clientside, using parts of a generic validation script I have re-used several times. I count the number of 'sets' the form contains, and have no problem looping through the fields using a standard 'for'-loop. However, when I pass the name of the document.formfield to the validation function, I get a 'is null or not an object' error I have tried to find a way to do this, but I need HELP! When I test and 'hardcode' one of the fieldnames in to the ValidDate function call, it does exactly what I want it to.... The line that kicks me out is: var elems = formField.value.split("."); Part of my code below: Code:
function validateForm(itemcount)
{
for (j=1;j <= itemcount; j++)
{
var formfieldname = 'document.frmLwAcftHistoryRegister.historydate_'+j +'';
formfieldvalue = eval(''+formfieldname+'.value');
if (formfieldvalue)
{
if (!validDate(formfieldname,"Date",true))
return false;
}
}
return true;
}
function validDate(formField,fieldLabel,required)
{
var result = true;
if (required && !validRequired(formField,fieldLabel))
result = false;
if (result)
{
var elems = formField.value.split(".");
result = (elems.length == 3); // should be three components
if (result)
{
var day = parseInt(elems[0]);
var month = parseInt(elems[1]);
var year = parseInt(elems[2]);
result = !isNaN(day) && (day < 32) &&
!isNaN(month) && (month < 13) &&
!isNaN(year) && (elems[2].length == 4);
}
if (!result)
{
alert('Enter a valid date in the field labeled "' + fieldLabel +'".');
formField.focus();
}
}
return result;
}
Andreas Last edited by stumpy : June 14th, 2004 at 07:15 AM. Reason: Please place code in code tags |
|
#2
|
|||
|
|||
|
Hi, guys.
After trying and failing and a lot of googling I found the solution myself: I now use: document.forms.frmLwAcftHistoryRegister[formfieldname] in the call to the validation function, and all seems to be well. Regards, Andreas |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Passing dynamic form field name to function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|