View Full Version : Trouble using an argument in a validation function

01-07-2004, 03:05 PM

I am trying to create a simple form validation check where the name of the form is passed as an argument to a validation function using an onclick event.

The function is placed on a page which creates several dynamic forms with dynamic names.

function checkuserprice(formname) {
if (eval(document.formname.extravagant.value == "")) {
window.alert("Please insert a price for the extravagant option.");
return false;}
return true;

The function returns a "null or not an object" error. The problem is that "document.formname.extravagant.value" is not being evaluated and I was wondering why not?

I have hard coded a form name into the function and it has worked as it should

Cheers for any help.


01-07-2004, 03:35 PM
Several things that I would correct. First things first, there's no need for the eval part in that code. You're not doing anything you could not do more effectively in other ways. As you might see if you perform a search for eval in this forum, you'll find a lot of discussions about why it's evil.

The second problem you have is your referencing. formname is an argument variable, but document.formname.extravagant.value is an identifier. That means that you are trying to access a form with the name 'formname' instead of the string that is contained by the variable formname. That problem is easily solved by the usage of document[formname].extravagant.value instead, or even better, document.forms[formname].elements['extravagant'].value.

01-08-2004, 03:34 AM

Your comments corrected my mistakes and the function is working.

Will look more into argument variables and identifiers.

Cheers for your help.