Formatting Strings

Those darn users often enter data in a haphazard fashion. If you want entries to follow a standard format, your best bet is to handle the formatting yourself. Script 8.5 shows how to take a list of names and convert them to standard capitalization format.

Script 8.5. This script takes a name entered in any format and replaces it with the capitalization you desire.
window.onload = initForms; function initForms() { for (var i=0; i< document.forms.length; i++) { document.forms[i].onsubmit = function() {return validForm();} } } function validForm() { var allTags = document.getElementsByTagName("*"); for (var i=0; i<allTags.length; i++) { validTag(allTags[i]); } return false; function validTag(thisTag) { var allClasses = thisTag.className.split(" ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.