Javascript : word capitalization

Posted on 2004-11-17
Last Modified: 2008-02-01
Does javascript have a function to take a string and capitalize the first letter of every word ?

I'd like to take a string in any format (all caps, all lowercase, etc.) and reformat to capitalize the first letter of every word.

Examples ?
Question by:Gitcho
    LVL 33

    Accepted Solution

    //credit to fritz the blank
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <SCRIPT LANGUAGE=javascript>
    function properName(strField)
    //This function adds capitals to each word in a string if the original text is
    //all upper case or all lower case
        var bolMixedCase, strValue, strTempValue, arrName, strProperName
        bolMixedCase = true;
        strValue = document.forms[0].elements[strField].value;
        strTempValue = strValue.toUpperCase()
             bolMixedCase = false;
        strTempValue = strValue.toLowerCase()
             bolMixedCase = false;
        if(! bolMixedCase){
             arrName= new Array(strValue.length)
                  arrName[intAt] = strValue.substring(intAt,intAt + 1);
             arrName[0] = arrName[0].toUpperCase()
             strProperName = arrName[0]
                  strTest = arrName[i-1];
                  if(arrName[i-1].toUpperCase()<"A" || arrName[i-1].toUpperCase()>"Z"){
                       arrName[i] = arrName[i].toUpperCase();
                       arrName[i] = arrName[i].toLowerCase();
                  strProperName = strProperName + arrName[i];
             eval('document.forms[0].' + strField + '.value=strProperName');


    <FORM action="" method=POST id=form1 name=form1>
    Full Name<INPUT type="text" id=text1 name=strName onBlur="properName('strName')">

    also check
    LVL 5

    Author Comment

    Right on the money ... thanks ...
    LVL 33

    Expert Comment

    Thanks for the points and grade

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    Join & Write a Comment

    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now