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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.