servlets thread: Use for loop to check validation in servlet.
You have errors in the javascript. It would be easier to read if you showed
the actual javascript output rather than having to read it through the
servlet.
As far as I can make out, this is the client side version of the script that
the browser sees (with added notes):
<SCRIPT LANGUAGE="JavaScript">
function validate()
{
var i;
for(i=1;i<4;i++) //check 3 text fields if they are empty
{
var d="Form.txtField"+i+".value" ; //NOTE1
if(d=="")
{
alert("Please enter a value for the text field.") //NOTE2
return(false);
} // end if loop
}// end for loop
return true //NOTE3
}// end function
A couple of points. First Notes 2 & 3 are on lines that don't terminate -
ie missing ";".
However the real problem appears to be Note 1 - var d:
var d="Form.txtField"+i+".value" ;
If my interpretation of the client side version is correct, then after this
the variable d will hold "Form.txtFieldi.value" with I representing the
current number. The problem is this is text so it doesn't look as the text
field at all. Hence every time this executes the:
if(d=="")
will never succeed as d is always something like "Form.txtFieldi.value".
Hope this helps,
Andrew
Professional Java Servlets 2.3
http://www.amazon.com/exec/obidos/ASIN/186100561X
http://www.amazon.co.uk/exec/obidos/ASIN/186100561X
-----Original Message-----
From: Hi Servlet [mailto:freeservlet@y...]
Sent: 03 August 2002 02:04
To: Servlets
Subject: [servlets] Use for loop to check validation in servlet.
Hi,
may i know what is the error in the below code:
out.println("<SCRIPT LANGUAGE=\"JavaScript\">");
out.println("function validate()");
out.println("{");
out.println("var i;");
out.println(" for(i=1;i<4;i++)"); //check 3 text fields if they are empty
out.println(" {");
out.println("var d=\"Form.txtField\"+i+\".value\" ;");
out.println("if(d==\"\") ");
out.println("{");
out.println("alert(\"Please enter a value for the text field.\")");
out.println("return(false);");
out.println("}"); // end if loop
out.println("}");// end for loop
out.println("return true");
out.println("}");// end function
How come it will not show the alert box with "Please enter a value for the
text field" if my text field is not entered?????
Thanks