parentNode.getElementsByTagName('span')[0];
}
span.innerHTML = validator.VALIDATIONS[fieldname]["error"];
} else {
// the regular expression *did* match
// is there a span.error already?
spans = fld.parentNode.getElementsByTagName('span');
if (spans.length == 0) {
// there is no error span, so do nothing
} else {
// remove the error span
span = fld.parentNode.getElementsByTagName('span')[0];
span.parentNode.removeChild(span);
// and remove class="error" on the field's label, if it has it
lbl = fld.parentNode.getElementsByTagName('label')[0];
if (lbl.className == 'error') {
lbl.className = lbl.className.replace(/\berror\b/,'');
}
}
}
}
}
The bold sections are those that have changed. The code to display an error message
simply, instead of assuming that a span exists, checks to see whether it does exist (by looking
for spans that are siblings of the text field). If it does not, the code creates the span with
createElement() and inserts it into the document. Similarly, the code to remove the error
message checks whether a span exists; if it does, the span is removed.
As a small extra wrinkle, the remove code also removes class="error" from the label if the
server code put it there.
Preventing the Form Being Submitted
Another usability enhancement is to prevent the form from being submitted if there are errors
present. It??™s generally good practice to stop the user from doing something if you know it??™s not
going to work.
Pages:
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231