David Wall wrote:
> > You need to use javascript. Here is a cheesy example:
> > THE SCRIPT
> > <script>
> > function checkFields() {
> > theValue = document.forms[0].whatever.value;
> > if (isNaN(theValue) == true) {
> > alert("Whatever must be numeric!");
> > document.forms[0].whatever.value = "";
> > }
> > }
>
> Is there a way to make the Javascript talk with the java beans at the time
> of JSP page creation? That is, suppose my bean has a list of valid values
> for a field. I want the Javascript to check that a field when POSTed is one
> of those valid values. How do I make the transfer of the information in the
> java bean into an Array or the like in Javascript?
>
The trick is that you can construct your JavaScript dynamically in a scriptlet,
based on your bean properties.
To do a really contrived example, let's say you have a bean "myBean" with two
properties -- getFirst() and getSecond() -- that return the two valid values for a
particular input field. These values got loaded from the database so they change
for each form. Therefore, you can't create a static JavaScript function with
fixed values. What you can do, though, is something like this (forgive me if my
JavaScript syntax is off ... I'm not as good at that as I am at Java):
<%
out.println("<script language=\"JavaScript\">");
out.println("function validateInput(field) {");
out.println(" if (field == '" + myBean.getFirst() + "')");
out.println(" return true;");
out.println(" else if (field == '" + myBean.getSecond() + "')");
out.println(" return true;");
out.println(" else");
out.println(" return false;");
out.println("}");
out.println("</script>");
%>
You can extend this approach to dynamically creating JavaScript arrays as you
build the page, or anything else you need.
In a JSP 1.1 environment, I'd be more likely to write a custom tag to emit this
code, but the basic idea is the same -- the code you create depends on the state
of your bean properties.
>
> David
>
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html