Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using eval() when passing variables into javascript 1

Status
Not open for further replies.

Ferrian

Programmer
Sep 7, 2005
53
GB
I am attempting to validate an ASP page using client-side javascript (so that I do not lose the data entered into the form). The validation is dynamic, as I never know how many rows will be returned in the ASP, the code looks like this:

function validateForm1(p_object)
{

for (eval("x=0;x<="+p_object+";x++"))
{
if(eval("document.form1.pod"+x+"_delstatus.value != "";"))
{
if(eval("document.form1.pod"+x+"_itemcount.value == "";"))
{
(eval("document.form1.pod"+x+"_itemcount.focus()"));
alert("You must enter an item count for this record");
return false;
}

if(eval("document.form1.pod"+x+"_deltime.value == "";"))
{
(eval("document.form1.pod"+x+"_deltime.focus()"));
alert("You must enter a delivery time for this record");
return false;
}

if(eval("document.form1.pod"+x+"_signature.value == "";"))
{
(eval("document.form1.pod"+x+"_signature.focus()"));
alert("You must enter an signatory name for this record");
return false;
}
}
}
}

The variable 'p_object' is a number.

When I attempt to run this function I get a generic Jscript error (Error: Expected ';') which hasn't helped me as I've gone through the code carefully and found no missing semi-colons or other errors I can find.

Can anyone help me? Thanks in advance.

Ben
 
what in the world?!

don't use eval!

Code:
function validateForm1(p_object)
{
    var elem = document.forms['form1'].elements;
    for (x=0; x<=p_object; x++))
    {
        if(elem['pod' + x + '_delstatus'].value != "")
        {
            if(elem['pod' + x + '_itemcount'].value == "")
            {
                elem['pod' + x + '_itemcount'].focus();
                alert("You must enter an item count for this record");
                return false;
            }

            if(elem['pod' + x + '_deltime'].value == "")
            {
                elem['pod' + x + '_deltime'].focus();
                alert("You must enter a delivery time for this record");
                return false;
            }

            if(elem['pod' + x + '_signature'].value == "")
            {
                elem['pod' + x + '_signature'].focus();
                alert("You must enter an signatory name for this record");
                return false;
            }
        }
    }
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Fair enough, this was my first attempt at including variables in Javascript.

Can I ask what the 'var elem = document.forms['form1'].elements;' is doing in this function? It seems to have replaced all the other references to the name of the ASP form later on, I'd like to understand this if you have the time.

Thanks again,

Ben
 
sure.

[tt]document[/tt] is the root object. it contains a collection of forms.

[tt]document.forms[/tt] refers to that collection of forms. you can reference a specific form using either its index (0-n, 0 being the first form), or it's name. [tt]document.forms['form1'][/tt] is a reference to your form named "form1".

[tt]document.forms['form1'].elements[/tt] refers to the form's elements, which include input fields, fieldsets and i think labels as well. similar to the form collection, you can reference these via index (0-n) or name. therefore, [tt]document.forms['form1'].elements['blah'][/tt] references the field named "blah" in your form named "form1".

so, what i did was create a variable named [tt]elem[/tt] to hold the array of elements within your form1. i then simply referenced each of them by name, as you were trying to do with the eval function.

hope this helps.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top