sanders
sanders

Reputation: 10898

Problem with eval function

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.

Upvotes: 0

Views: 323

Answers (3)

Eldar Djafarov
Eldar Djafarov

Reputation: 24735

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]

Upvotes: 2

bobince
bobince

Reputation: 536685

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]];

Upvotes: 2

James
James

Reputation: 111980

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

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

Upvotes: 2

Related Questions