SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 213 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

It looks like this:
function checkField(e) {
fld = window.event ? window.event.srcElement : e.target;
fieldname = fld.id;
if (VALIDATIONS[fieldname]) {
re = VALIDATIONS[fieldname]["regexp"];
if (fld.value.search(re) == -1) {
// the regular expression didn't match
// find the span.error element for this field
// and put the error message in it
span = fld.parentNode.getElementsByTagName('span')[0];
span.innerHTML = VALIDATIONS[fieldname]["error"];
} else {
// the regular expression *did* match
// remove the error message!
span = fld.parentNode.getElementsByTagName('span')[0];
span.innerHTML = "";
}
}
}
First, since this is an event handler, you need to get the element that fired the event??”
in this case, that element will be the text field itself, which is what you care about. You use a
little cross-browser coding to get a reference to the element, using the window.event object
in browsers that provide it (Internet Explorer) and the World Wide Web Consortium (W3C)
event object in other browsers.
CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 151
Following that, you look in the VALIDATIONS object to see whether there??™s a regexp defined
for this field by checking whether VALIDATIONS[fieldname] exists. If it does, you retrieve the
regexp defined in VALIDATIONS, ready for checking.
Text fields have their current value available in fieldobject.value, which is a string, and
strings (as noted previously) have a search() method to check the string against a regexp that
returns -1 if there??™s no match.


Pages:
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6