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 216 | Next

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

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

getElementById) return;
// Walk through the VALIDATIONS list and for each one find the field
// it applies to and attach an onBlur handler, so when the user leaves
// the field, it checks the contents of the field against the regexp.
for (fieldname in validator.VALIDATIONS) {
fld = document.getElementById(fieldname);
if (!fld) continue; // ignore this field if it doesn't exist in the page
validator.addEvent(fld, "blur", validator.checkField);
}
},
checkField: function(e) {
fld = window.event ? window.event.srcElement : e.target;
fieldname = fld.id;
if (validator.VALIDATIONS[fieldname]) {
re = validator.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 = validator.VALIDATIONS[fieldname]["error"];
} else {
// the regular expression *did* match
// remove the error message!
span = fld.parentNode.getElementsByTagName('span')[0];
span.innerHTML = "";
}
}
},
CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 154
addEvent: function( obj, type, fn ) {
if (obj.addEventListener) {
obj.addEventListener( type, fn, false );
} else if (obj.attachEvent) {
obj["e"+type+fn] = fn;
obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
}
}
}
validator.


Pages:
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228