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

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

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

addEvent(fld, "blur", validator.checkField);
}
validator.checkForErrors();
},
checkField: function(e) {
...
// finally, disable or enable the submit button as appropriate
validator.checkForErrors();
},
checkForErrors: function() {
// Look for span.error in the page
var spans = document.getElementsByTagName('span');
for (var i=0; i// does this span have class=error?
if (spans[i].className.match(/\berror\b/)) {
// disable the submit button and exit
document.getElementById("submitButton").disabled = true;
return;
}
}
// there were no span.error elements, so enable the submit button
document.getElementById("submitButton").disabled = false;
},
addEvent: function( obj, type, fn ) {
...
}
}



A simple PHP form using regular expressions for validation



...





CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 159
CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 160
Form Validation with Ajax
Regular expressions are a powerful tool, but they do have their limitations. In the previous
example, the date expression was ^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$, which allows two digits, a
separator, two digits, a separator, and four digits. This will correctly block something similar to
???I??™m not telling you my date of birth???, but it will merrily allow such invalid monstrosities as
???99/99/9999???, ???32/01/1995???, and ???29/02/2007???.


Pages:
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234