JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 14th, 2004, 07:02 AM
Wish4ItAll Wish4ItAll is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 Wish4ItAll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cool Passing dynamic form field name to function

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;
}
Regards,
Andreas

Last edited by stumpy : June 14th, 2004 at 07:15 AM. Reason: Please place code in code tags

Reply With Quote
  #2  
Old June 14th, 2004, 09:05 AM
Wish4ItAll Wish4ItAll is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 Wish4ItAll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Found the solution!

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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Passing dynamic form field name to function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT