Java Reference
In-Depth Information
Figure 15.16:
Form being validated on the client side with a pop-up
error message
Simulating Client-Side Validation with a Little Help from the Server
Client-side validation is nice, but you saw how it does have some limi-
tations. Another strategy is to simulate client-side validation by issuing
Ajax requests as the user is filling out the form, using the responses to
show error messages instantly. The advantage of doing this is that we're
reusing the server-side validation code; the drawback is that we're con-
stantly sending and receiving data between the client and the server,
so we're no longer saving bandwidth—quite the opposite. Nevertheless,
let's have a look at this technique.
First we'll add a call to a JavaScript method in the onkeyup= event of
the text fields:
Download ajax/web/WEB-INF/jsp/server_side_validation.jsp
<s-dyn:text id="contact.email" name="contact.email"
autocomplete="off" onkeyup="validate(this.form);"/>
We'll also surround the <s:errors> tag with a container so that we have
a place to put validation error messages:
Download ajax/web/WEB-INF/jsp/server_side_validation.jsp
<span id="contact_email" class="error">
<s:errors field="contact.email"/>
</span>
 
 
Search WWH ::




Custom Search