Capiataloze Words - not working in IE

Posted on 2008-11-04
Medium Priority
Last Modified: 2012-05-05
I ma using teh following to capitalize letters in words, works great except in ie, in ie the divider is not retained,

so in ie sean micheal becomes Seanmicheal

function capitalizeMe(objFormField) {
      var arrWords=objFormField.value.toLowerCase().split(/(\s+|\-|\.|\')/);
      objFormField.value = arrWords.join('');
Question by:James Rodgers
LVL 15

Expert Comment

ID: 22880931
why dont you try:
<script type="text/javascript">
function capWords() {
var inputString = document.form1.instring; // The input text field
var outputString = document.form1.outstring; // The output text field
var tmpStr, tmpChar, preString, postString, strlen;
tmpStr = inputString.value.toLowerCase();
stringLen = tmpStr.length;
if (stringLen > 0)
  for (i = 0; i < stringLen; i++)
    if (i == 0)
      tmpChar = tmpStr.substring(0,1).toUpperCase();
      postString = tmpStr.substring(1,stringLen);
      tmpStr = tmpChar + postString;
      tmpChar = tmpStr.substring(i,i+1);
      if (tmpChar == " " && i < (stringLen-1))
      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
      preString = tmpStr.substring(0,i+1);
      postString = tmpStr.substring(i+2,stringLen);
      tmpStr = preString + tmpChar + postString;
outputString.value = tmpStr;
<body id="doc">
	<form name="form1" method="post">
<input name="instring" type="text" value="this is the text string" size="30">
<input type="button" name="Capitalize" value="Capitalize >>" onClick="capWords();">
<input name="outstring" type="text" value="" size="30">

Open in new window

LVL 25

Accepted Solution

James Rodgers earned 0 total points
ID: 22881906
if you look at my code you will see that i need it to work with more than just spaces, i also need it to work with apostrophe, hyphen and periods, that's the problem i can get it top work with jst spaces but adding in the other items makes it non functional in IE

i have replaced my code with this

function setProperCase(objFormField) {
      objFormField.value=objFormField.value.toLowerCase().replace(/(^\s*\w|[\s\.\-\']\s*\w)/g,function(c){return c.toUpperCase()});

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 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