0

Hello I get a weird error in this code:

 function validateForm(aform) {
  var s="";

  //check if form has validator arr
  if (aform.validatorArr)  {
    //Iterate over Form elements
    for (var i=0;i<aform.validatorArr.length;i++)  {
      eval("var anelem=document.forms."+aform.name+"."+aform.validatorArr[i][1]);
      var pattern=getPatternByName(aform.validatorArr[i][2]);

      if (aform.validatorArr[i][4]=="radio") {
        var fv="";

        eval("var chkArray=aform."+aform.validatorArr[i][1]+";");

        for (j=0;j<chkArray.length;j++)  {
          if (chkArray[j].checked) {
            fv=chkArray[j].value;
          }
        }

        if (validateValue(fv,pattern)==false)  {
          s+=aform.validatorArr[i][3]+"\n";
        }

      } else {
        if (validateField(anelem,pattern)==false)  {
          s+=aform.validatorArr[i][3]+"\n";
        }
      }
    }

    //Report errors
    if (s!="") {
      alert(s);
      return false;
    }

  }

  return true;
}

missing ] after element list Line 217

This is line 217:

 eval("var anelem=document.forms."+aform.name+"."+aform.validatorArr[i][1]);

Any idea's what's wrong?

Update

This is how de function is called: submitForm('formbuilder_form'); Which is identical to the form name:

    <form name="formbuilder_form" method="POST" action="processform.php" style="margin:0px">

PROBLEM SOLVED

In onw of the form element someone decided it was good to have it like this:

Js doesn't like this.

Thanks for helping everyone.

1
  • btw, you don't need to use eval: document.forms[aform.name][aform.validatorArr[i][1]] should work just fin. Commented Sep 3, 2009 at 12:36

3 Answers 3

2

Why you do this? You should do this:

var anelem=document.forms[aform.name][aform.validatorArr[i][1]];

But from updated post it is understood that aform is a string - not an object. So you should get an object first.

aform=ocument.forms[aform]
Sign up to request clarification or add additional context in comments.

4 Comments

Even better - document.forms[aform.name].elements[aform.validatorArr[i][1]];
Error means you have no form named aform.name on the page.
how do you call validateForm(aform)
from a submit button within the form
2

There's no need to use eval. Try this instead:

var anelem = document.forms[aform.name][aform.validatorArr[i][1]];

1 Comment

this gives me the error: document.forms[aform.name] is undefined Line 218
2

forms.fish is the same as forms['fish'] for all JavaScript objects. So you can use []s to get a property whose name is stored in a string from an object instead of evil eval.

Top tip: if you find yourself ever using eval, you are probably making a mistake.

"document.forms."+aform.name

is particularly comical because you're trying to get the name of a form object and access the form with that name — which is the same form object you started out with!

However:

submitForm('formbuilder_form')

is wrong whether you're using eval or []: you're passing a string into submitForm instead of a form object. A string does not have a name property.

And having submitForm separate from the normal form submission process is messy and breaks non-JavaScript UAs. If you write:

<form method="post" action="processform.php" onsubmit="return submitForm(this);">
   ...
</form>

then 'aform' in the function will be the form object and you can access elements as simply as:

var anelem= aform[aform.validatorArr[i][1]];

without ever having to worry about document.forms, scripting submit buttons or form names. Although, to avoid name clashes on form properties, this is better:

var anelem= aform.elements[aform.validatorArr[i][1]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.