Converting Value Case

I can use
onChange="this.value=this.value.toLowerCase();"
to convert to lowercase and
onChange="this.value=this.value.toUpperCase();"
to convert to uppercase.

What can I use to capitalise a value?

ie.
joe bloggs OR Joe bloggs OR joe Bloggs
would become:
Joe Bloggs
lhuttonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dvalineCommented:
The value parameter is stored as a variant item.  Not a string the toUpperCase is a method of the String object.  This is how it should be done.

function ChangeToUpper(TextBox) {
  var sTemp = new String(TextBox.value);
  TextBox.value = sTemp.toUpperCase;
}

onChange="ChangeToUpper(this)"

or even better

  var cUpper = 1;
  var cLower = 2;
 
function ChangeCase(TextBox, ChangeType) {
  var sTemp = new String(TextBox.value);
  if (ChangeType == 1) {
    TextBox.value = sTemp.toUpperCase;  
  }
  if (ChangeType == 2) {
    TextBox.value = sTemp.toLowerCase;
  }
}

onChange="ChangeCase(this, cUpper)"
0
lhuttonAuthor Commented:
I tried the both codes you gave and the error produced was:



function toUpperCase() {
    [native code]
}
0
dvalineCommented:
Sorry I fixed the code

The value parameter is stored as a variant item.  Not a string the toUpperCase is a method of the String object.  This is how it should be done.

function ChangeToUpper(TextBox) {
  var sTemp = new String(TextBox.value);
  TextBox.value = sTemp.toUpperCase();
}

onChange="ChangeToUpper(this)"

or even better

  var cUpper = 1;
  var cLower = 2;
   
function ChangeCase(TextBox, ChangeType) {
  var sTemp = new String(TextBox.value);
  if (ChangeType == 1) {
    TextBox.value = sTemp.toUpperCase();  
  }
  if (ChangeType == 2) {
    TextBox.value = sTemp.toLowerCase();
  }
}

onChange="ChangeCase(this, cUpper)"
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

lhuttonAuthor Commented:
Your lengthy script converts a value to uppercase, which can be done simply using
onChange="this.value=this.value.toLowerCase();"

I need to be able to capitalise a value.
0
Jan LouwerensSoftware EngineerCommented:
onChange="capitalize(this)"

function capitalize(element)
{
   var result = '';
   var wordArray = element.value.split(' ');
   for (index = 0; index < wordArray.length; index++)
   {
      // make wordArray[index][0] a capital letter!  ???
      // alert(wordArray[index]);
      if (index > 0)
         result += ' ';
      result += wordArray[index];
   }

   element.value = result;
}


.... this isn't a complete answer. All you have to do is capitalize the first letter of each word. (I tried, but toUpperCase() didn't work... is there another way besides comparing ascii values?)
0
nettromCommented:
lhutton: do you want a solution that capitalizes the first letter of a word, or the first letter of every word in a string?
0
lhuttonAuthor Commented:
I need a solution that will capitalise the first letter of every word in a string.

I'd also like to know whether I should be using this script:

<!--
    var cUpper = 1;
    var cLower = 2;

    function ChangeCase(TextBox, ChangeType) {
      var sTemp = new String(TextBox.value);
      if (ChangeType == 1) {
        TextBox.value = sTemp.toUpperCase();
      }
      if (ChangeType == 2) {
        TextBox.value = sTemp.toLowerCase();
      }
    }
// -->

onChange="ChangeCase(this, cUpper)"

instead of what I have been using:

onChange="this.value=this.value.toLowerCase();"
onChange="this.value=this.value.toUpperCase();"

If it's possible to incorporate the solution that will capitalise the first letter of every word in a string into the script above, that would be good.
0
Jan LouwerensSoftware EngineerCommented:
I don't think that script does anything useful besides make the entire string all caps or all lower case... the script I wrote above seperates the string into seperate words which you can use to capitalize each word, and then joins the capitalized words back into the string...
0
Jan LouwerensSoftware EngineerCommented:
if you're having trouble capitaling the words, let me know and I'll figure out how to do it...
0
lhuttonAuthor Commented:
I tried your script but it didn't do anything to the string. From the comment you made, I thought you couldn't get it to work???
0
Jan LouwerensSoftware EngineerCommented:
yeah, it doesn't do anything to the string as is, but where I have the comment:
 // make wordArray[index][0] a capital letter!  ???
is where you need to capitalize the first letter of the word. If you can't figure out how to do it, I can figure it out. Either by using toUpperCase() or by comparing the actual ascii value...
0
lhuttonAuthor Commented:
I have no idea what you mean???

I'm looking for a script to capitalise the first letter of each word. I don't know how to do it myself - that's why I'm asking???
0
Jan LouwerensSoftware EngineerCommented:
ok, I'll figure it out and post it here... give me a few minutes, ok?  =)
0
Jan LouwerensSoftware EngineerCommented:
ok, I'll figure it out and post it here... give me a few minutes, ok?  =)
0
Jan LouwerensSoftware EngineerCommented:
function capitalize(element)
{
   var result = String();
   var sentence = element.value.toLowerCase();
   var wordArray = sentence.split(' ');
   for (word = 0; word < wordArray.length; word++)
   {
      var tmp = wordArray[word][0];
      for (letter = 0; letter < wordArray[word].length; letter++)
         tmp += wordArray[word][letter];
      if (word > 0)
         result += ' ';
      result += tmp;
   }
   element.value = result;
}

onChange="capitalize(this)"
0
Jan LouwerensSoftware EngineerCommented:
Oops!!! The inner loop should be:

for (letter = 1; letter < wordArray[word].length; letter++)
   tmp += wordArray[word][letter];

sorry about that!!
0
lhuttonAuthor Commented:
This script changes the entire string to lower case and is no different from those which have been given. I need to be able to convert:
joe bloggs
to:
Joe Bloggs
not:
JOE BLOGGS
0
Jan LouwerensSoftware EngineerCommented:
no, it does more than that... it only makes it all lower case in the beginning so that it can convert:
jOe blOGgS
to:
Joe Bloggs

Did you try running the script and seeing the result? I tested it myself and it worked great for me. Let me know if you still have problems with it...
0
Jan LouwerensSoftware EngineerCommented:
WAIT!!  I made another typo!!
This line:
var tmp = wordArray[word][0];

should be:
var tmp = wordArray[word][0].toUpperCase();

Sorry again!! Darn my sloppy fingers!!!
NOW it should work for you
0
lhuttonAuthor Commented:
Works now :-) One more thing... what browsers will support the script and how can I prevent errors/problems in browsers that don't?
0
lhuttonAuthor Commented:
One error:
wordArray[word][0] has no properties.
appears when the string is removed from the field and left blank.
0
Jan LouwerensSoftware EngineerCommented:
oh, ok...
after this line:
var sentence = element.value.toLowerCase();

place this:
if (sentence.length > 0)
{
}

put the closing brace right after the line:
element.value = result;
0
lhuttonAuthor Commented:
OK, works now. Now, just the question of compatibility and also can I incorporate the function that was given earlier to change to lower or upper case into this script (to keep things tidier)?
0
Jan LouwerensSoftware EngineerCommented:
well, if javascript isn't supported, I don't think there's really anything you can do...
about the other function... I don't really think it's necessary at all... you might as well just use toUpperCase() and toLowerCase() by themselves...
which part do you think it would make tidier? looks like extra, unneccesary code to me...
0
lhuttonAuthor Commented:
OK, I was originally using
onChange="this.value=this.value.toLowerCase();"
but was told that was wrong (see Wednesday, January 26 2000 - 12:00PM NZDT)

What should I use?
0
Jan LouwerensSoftware EngineerCommented:
you should just use:
onChange="capitalize(this)"
0
lhuttonAuthor Commented:
Yeah, that's for strings in fields that I want capitalised. But, there are strings in fields that need to be set solely to lowercase and others solely to uppercase.
0
Jan LouwerensSoftware EngineerCommented:
then what you were originally using should work just fine. Was it working for you? If it was, I say just leave it that way. If you really want them in functions, you can use:

function uppercase(element)
{
   element.value = element.value.toUpperCase();
}

function lowercase(element)
{
   element.value = element.value.toLowerCase();
}


onChange="uppercase(this)" -> for UpperCase
onChange="lowercase(this)" -> for LowerCase

to make it look more like the other capitalize example.
0
lhuttonAuthor Commented:
Thank you for all your help. Much appreciated!
0
Jan LouwerensSoftware EngineerCommented:
no problem. glad you got it workin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhuttonAuthor Commented:
Hey, I just noticed (didn't think of checking before) that the script doesn't work in MSIE 5.0. How can I get it to work in MSIE also?
0
Jan LouwerensSoftware EngineerCommented:
do you happen to know at which part it's failing? I'm on Linux, and don't have IE, so I can't test it myself...
0
lhuttonAuthor Commented:
The error being reported is:

Line: 46
Char: 9
Error: '0' is not an object
Code: 0
0
Jan LouwerensSoftware EngineerCommented:
well, without seeing your code that doesn't really help me... can you post the line it's on?
0
lhuttonAuthor Commented:
I'm guessing it's this line:
var tmp = wordArray[word][0].toUpperCase();

but I can't be sure exactly where line 46 is. When I go to "what I think is line 46" there's no '0' in it???

So, I'm assuming the '0' in this line is the problem object.
0
Jan LouwerensSoftware EngineerCommented:
the zero there is just a number (index in to the array), not an object... weird...
....perhaps it's a precedence problem... try this maybe:
var tmp = String(wordArray[word][0]).toUpperCase();
0
lhuttonAuthor Commented:
That puts "UNDEFINEDundefinedundefinedundefined" into the text box.
0
lhuttonAuthor Commented:
So, the only other possible lines are:

if (sentence.length > 0)

for (word = 0; word < wordArray.length; word++)

if (word > 0)
0
Jan LouwerensSoftware EngineerCommented:
ok, well, when it put "UNDEFINDED..." in the text box, did the error go away? Is it running ok now, just giving bad results?
0
nettromCommented:
may I suggest the following solution?  I found it to work perfectly well in IE4 and Netscape 4.x under NT4.  let me know if it fails. :)

<script type="text/javascript">
function capitalize(myElement) {
  var returnString = myElement.value.charAt(0).toUpperCase();
  for(i = 1; i < myElement.value.length; i++) {
    if(myElement.value.charCodeAt(i-1) == 32) {
      returnString += myElement.value.charAt(i).toUpperCase();
    } else {
      returnString += myElement.value.charAt(i);
    }
  }
  myElement.value = returnString;
}
</script>

invoked as the other suggestion, example:

<input type="text" onchange="capitalize(this)">
0
lhuttonAuthor Commented:
This new script works fine in NC 4.7 and NSIE 5.0!!! Thanks :-)

Any reason why the previous one wouldn't work in MSIE 5.0?
Also, do you know of any versions that might have a problem with this one? If so, can I prevent any problems?
0
lhuttonAuthor Commented:
Also noticed that this new script will only capitalise the first first letter of a word - it won't force the rest of the letters to lowercase, like the old one...
0
Jan LouwerensSoftware EngineerCommented:
thanks, nettrom...
trying to debug the other without IE myself was proving to be no easy task...

lhutton, I was making all the other letters lowercase on purpose... I thought that was what you wanted. Changing the line:
var sentence = element.value.toLowerCase();
to:
var sentence = element.value;
would have fixed that. However, thanks to nettrom, that point is moot now...
0
nettromCommented:
I haven't got a large collection of browsers here at home, but I tested it in IE4 and Netscape 4.7 as mentioned, and I've also got IE5 installed here now.  all browsers run it as they should.  The script doesn't work in Opera, but that's no suprise since Opera's JavaScript-handling is somewhat non-existant at times.  Netscape 4.61 under Linux also worked as it should.

the error I get in IE5 with the old script is:

Error: '0' is not an object.

it seems like IE has trouble working with a string as an array of characters, and therefore doesn't like the wordArray[word][0] way of indexing the characters in the string.

that's all the info I have right now.  I can do some more extensive quality assurance on Monday when I'm back at work, but I wouldn't expect any huge problems.
0
lhuttonAuthor Commented:
Thank you very much for your input. It's much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.