For the first scenario I mentioned (using JavaScript to validate your
form), you could have a page that looks like:
<FORM NAME=LoginForm ACTION=TheServletToSendValidDataTo METHOD=post>
<INPUT TYPE=TEXT NAME=UserName VALUE="">
<INPUT TYPE=PASSWORD NAME=Password VALUE="">
<INPUT TYPE=BUTTON VALUE=Login onClick="validateInput();">
</FORM>
<SCRIPT LANGUAGE=JavaScript>
function validateInput()
{ if (fieldIsEmpty(LoginForm.UserName))
{ alert("Please enter your user name.");
}else if (fieldIsEmpty(LoginForm.Password))
{ alert("Please enter your password.");
}else
{ LoginForm.submit();
}
}
function fieldIsEmpty(whichField)
{ if (whichField == "")
{ return true;
}else
{ return false;
}
}
</SCRIPT>
Of course your JavaScript validation can be more more complicated -- I
kept it simple for the sake of example.
For the second scenario (having the validation servlet reload the form
with the entered data and pop up an alert message), try something like
this:
(This example assumes the form is in a static HTML page)
When the validation servlet detects invalid data, redirect to the form,
attaching all the form data to the URL so you get something like:
UserLogin.html?UserName=John%20Doe&Password=&errorMessage=Please%20enter
%20your%20password.
Notice I've also added a value called errorMessage. In the HTML file,
use JavaScript to parse the values and do like:
document.writeln("<INPUT TYPE=TEXT NAME=UserName VALUE=\"" + userName +
"\">
At the end, do a test like
if (errorMessage != "")
{ alert(errorMessage);
}
If the form is in a servlet, it's pretty much the same as above except
you can obviously populate the form without using JavaScript. At the
end of the output, supply that alert(errorMessage); JavaScript if
errorMessage is anything but empty.
I can't provide any more detailed code than that right now, but
these examples should get you on your way. If you choose the first
method to solve your problem, know that any book on JavaScript will show
you how to validate data in a form before submitting it. The second
method, if the form is in a servlet, is plain ol' servlet work with the
addition of the errorMessage JavaScript alert being added to the HTML
page. If the form is in a static page, you'll need JavaScript to parse
the query string, a topic that I went into detail about a couple of
weeks ago in a discussion about passing values from servlets to HTML
pages. Check the archives if you want to know more about that,
including sample code.
Hope this all helps you!
Russell Montgomery
Digital Renaissance
> -----Original Message-----
> From: Satish Talim [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, March 02, 1999 7:03 PM
> To: Russell Montgomery
> Subject: Re: Retaining current page after post?
>
> Hi,
>
> Any sample code to help with concepts you mentioned below.
>
> Thanks,
>
> Satish
>
>
> At 10:49 AM 3/4/99 -0500, you wrote:
> > Depending on what kind of validation you do, you may be able to
> do
> >it within the page itself using JavaScript. When the user clicks the
> >Submit button, check that required fields have been filled in, that
> >numeric fields contain numbers, etc. If validation passes, submit to
> a
> >servlet, otherwise throw up an alert box telling the user what's
> wrong.
> >
> > Another option is to do the validation in the servlet the form
> is
> >submitted to, and if things don't meet your requirements, submit the
> >values that were passed to the validation servlet back to the form
> and
> >have the form populate itself with these values. Include a value
> such
> >as errorMessage which, if not null, causes that form to display an
> alert
> >box containing the message.
> >
> >
> >Russell Montgomery
> >Digital Renaissance
> >
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html