The code stores away a reference to this in a variable thisfield, so
that it??™s still available in the callback.
A second wrinkle is that it??™s possible to create a jQuery query object from an existing variable
by writing $(variable); this is used to make a query object from the stored thisfield
variable.
The code makes use of another jQuery method: siblings(). This does the same thing as
the fld.parentNode.getElementsByTagName('span')[0] part of the regular expression??™s code,
but in a much easier-to-read way (I??™m sure you??™ll agree). The siblings() method by itself will
return all the other children of the same parent node, but you can pass a Cascading Style
Sheets (CSS) selector as before to limit it to only the siblings you care about.
Finally, the empty() method is used to remove the contents of the error span, and the
append() method fills in the error span with the error message passed back from the server
if there was one.
Using Ajax to handle form validation is a convenient way to make your forms more
usable; you avoid a page refresh and having to wait for the server to resend the whole page,
but still take advantage of all the complexity and power of the code running on the server to
make your validation routines as comprehensive as you want.
Summary
Form validation is important. If you care about the quality of your data, the best thing to do
is to try and enforce that quality as much as possible.
Pages:
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240