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

client side validation

Status
Not open for further replies.

parames

MIS
Jun 26, 2000
71
MY
hi list.. need help on validation

My asp code looks like this:
----------
<form method=post name=&quot;form&quot; action=&quot;order_insert.asp&quot; onSubmit=&quot;return validate(this)&quot; >

----------
<%
iCount=10
for i=1 to iCount
iCount=iCount+1 %>

<tr bgcolor=#eeeeee>
<td><b><%=i%></b></td>
<td><input type=text name=&quot;item_code<%=i%>&quot; size=15></td>
<td><input type=text name=&quot;stock_item<%=i%>&quot; size=6></td>
<td><input type=text name=&quot;description<%=i%>&quot; size=30></td>
<td><input type=text name=&quot;order_qty<%=i%>&quot; size=5>&nbsp;
</tr>

<%
Next
%>
</table>
<center><br><input type=submit name=&quot;button&quot; value=&quot;Submit&quot;>
<input type=reset value=&quot;Reset&quot;>
----------

The problem is i don't know how to do validation in javascript using for loop.. i've tried something like this :
------------
var x;

for (x=1; x<11; x++)
{
if(document.form.description+x.value == '')
{
alert (&quot;description empty&quot;);
document.form.description+x.focus();
return false;
}
}
-------

1) It never prompt any alert message although the description field is empty/not empty.

2) If i use 'if(document.form.description+x.value != '')' then,it prompt alert box if the fields are empty/not empty

3) If i use 'document.form.description+x.focus();' ,it shows error message &quot;Object doesn't support this property or method&quot;

I'm really going crazy with this. Please help me..

thanks a lot,
rgds,
parames.s


 
I may be wrong on this, I'm a little rusty on my javascript, but I think I might have an answer.

first off,
because you're defining x within the loop as a integer, x will have no properties, therefore x.value holds the value 1,2,3,... or 11. That's why x.focus won't work, because you aren't referencing the form correctly.

what I've done in the past is use the eval function to determine stuff like this. what the eval function does is it takes whatever is inside the (), and evaluates it to true or false. If it evaluates to true, and there is an assignment operator inside, it performs the statement.

ex:
Code:
var temp = eval('document.form.description'+x+'.value == &quot;&quot;');
if (temp)
...

then to do the focus, again use the eval function such as:
Code:
eval('document.form.description'+x+'.focus()');

I'm really not sure if this will work, because I've never made a function call within the eval statement.

I think that the easiest way to do this would be as follows:
Code:
var x;

for (x=1; x<11; x++)
{
 var objRef;
 eval('objRef = document.form.description' +x);
   if(objRef.value == '')
    {
        alert (&quot;description empty&quot;);
        objRef.focus();
        return false;
    }
}

hope this helps, and i especially hope this made sense. I'm a little sick right now.

good luck

ps:
when debugging javascript, get the Microsoft Script Debugger and the Netscape Javascript Debugger, they can save you a whole lot of time, because they allow you to step through your code. Both are available at their respective websites.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top