[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 856
  • Last Modified:

Javascript : word capitalization

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 ?
0
Gitcho
Asked:
Gitcho
  • 2
1 Solution
 
sajuksCommented:
//credit to fritz the blank
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<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()
    if(strValue==strTempValue){
         bolMixedCase = false;
    }
    strTempValue = strValue.toLowerCase()
    if(strValue==strTempValue){
         bolMixedCase = false;
    }
    if(! bolMixedCase){
         arrName= new Array(strValue.length)
         for(intAt=0;intAt<strValue.length;intAt++){
              arrName[intAt] = strValue.substring(intAt,intAt + 1);
         }
         arrName[0] = arrName[0].toUpperCase()
         strProperName = arrName[0]
         for(i=1;i<arrName.length;i++){
              strTest = arrName[i-1];
              if(arrName[i-1].toUpperCase()<"A" || arrName[i-1].toUpperCase()>"Z"){
                   arrName[i] = arrName[i].toUpperCase();
              }else{
                   arrName[i] = arrName[i].toLowerCase();
              }
              strProperName = strProperName + arrName[i];
         }
         eval('document.forms[0].' + strField + '.value=strProperName');
    }
}
//-->
</SCRIPT>

</HEAD>
<BODY>

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


also check  http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20892916.html
0
 
GitchoAuthor Commented:
Right on the money ... thanks ...
0
 
sajuksCommented:
Thanks for the points and grade
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now