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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form help: Checking values

Status
Not open for further replies.

Castanz

Programmer
Apr 5, 2002
61
US
I am using the following code and the frm.fld.value gives an execution error: frm.fld has no properties. I know the variable "fld" gets passed correctly, but I can't put the value of fld in the frm.fld.value line. Is there anyway to make the fld value take the place of the variable name? Can anyone help?

Thanks,
castanz@yahoo.com

<script type=&quot;text/javascript&quot;>
<!--
function Check_Each_Entry(frm,fld)
{
// Check if the comment field has an entry. If it doesn't,
// show an error message, and set focus back to the field.
if (frm.fld.value != &quot;&quot;)
{
return false;
}
alert('This field is a required field');
frm.fld.focus();
return true;
}
function checkEntries(frm)
{
// Make Array for all fields. -->
FieldList = new Array (&quot;Date&quot;, &quot;Time&quot;, &quot;DateReport&quot;,&quot;status&quot;, &quot;gender&quot;, &quot;LastName&quot;, &quot;FirstName&quot;, &quot;SSN&quot;, &quot;Rate&quot;, &quot;Age&quot;, &quot;Birthdate&quot;)
var Entry_Err = false

// Loop and find the first error. -->
for (i=0; i<=11; i++) {
fld = FieldList;
if (Entry_Err == false)
{
Entry_Err = Check_Each_Entry(frm,FieldList)
alert(&quot;Field: &quot; +fld)
}
} //End For Statement -->

// If there is an error, return false. -->
if (Entry_Err == true)
//{
return false
//}
return true
} //End Function -->
//-->
</script>
 
how do u call the functions?

Known is handfull, Unknown is worldfull
 
I call them by a submit button in the form line:
<form id=&quot;inputForm&quot; name=&quot;inputForm&quot; method=&quot;post&quot; action=&quot;test.asp&quot; onsubmit=&quot;return checkEntries(this)&quot;>

Maybe there is a better way of checking many values in a form. Any suggestions?

Thanks in advance,
Al
 
That looks ok but 1 little problem
Code:
This function needs changes.
Your fields are not received by reference so your fld wont work till you dont change it.

   function Check_Each_Entry(frm,fld)
    {
        // Check if the comment field has an entry. If it doesn't, 
        // show an error message, and set focus back to the 
field.
fld=frm[fld];
Code:
        if (frm[fld]value != &quot;&quot;)
        {
            return false;
        }
        alert('This field is a required field');
            frm[fld]focus();
        return true;
    }

You need to access the form fields collection using theyr names not references. That was the problem.
frm.fld should be frm[fld]

________
George, M
 
Thanks for the help. frm[fld] gave a value of undefined. In the statement if (frm.fld.value != &quot;&quot;), I tried putting the SSN field in place of fld (i.e. frm.SSN.Value) and the code works fine. However, I can't use a loop to check all fields.
 
My mistake it should be this way

function Check_Each_Entry(frm,fld)
{
// Check if the comment field has an entry. If it doesn't,
// show an error message, and set focus back to the
field.

fld=frm[fld];

if (fld.value != &quot;&quot;)
{
return false;
}
alert('This field is a required field');
fld.focus();
return true;
}


________
George, M
 
I haven't had the chance to try this but how about:
if( &quot;frm.&quot; + fld + &quot;.value&quot;) is passed?
 
That's a string and it's always true.
You have to use eval to execute a dynamic code(string variable that contains the code)

if(eval(&quot;frm.&quot; + fld + &quot;.value&quot;))...

________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top