gibney.org
:
Technology
:
Javascript
:
Includes
:
capitalizeFormFields
(Entry Nr. 256, by user 1 |
edit
)
function ge_string_capitalizeWords (str) { var lastChar=' '; // so the first character of str is made uppercase var strLength=str.length str=str.toLowerCase(); // all lowercase // now make the chars following a ' ' or '-' uppercase for(var i=0; i<strLength; i++) { if (lastChar==' ' || lastChar=='-') { str=str.substr(0,i)+str.substr(i,1).toUpperCase()+str.substr(i+1); } lastChar=str.substr(i,1); } return str; } function ge_capitalizeFormFields(formId) { if (!document.getElementById) return; // no support for getElementById theForm=document.getElementById(formId); if (!theForm) return; // there is no element with formid for (var formField=0; formField<theForm.length; formField++) { if (!theForm.elements[formField]) continue; if (!theForm.elements[formField].className) continue; if (theForm.elements[formField].className.indexOf('capitalize')==-1) continue; if (!theForm.elements[formField].value) continue; theForm.elements[formField].value=ge_string_capitalizeWords(theForm.elements[formField].value); } }
Create a new entry at this position