Link to home
Start Free TrialLog in
Avatar of lhutton
lhutton

asked on

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
Avatar of dvaline
dvaline

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)"
Avatar of lhutton

ASKER

I tried the both codes you gave and the error produced was:



function toUpperCase() {
    [native code]
}
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)"
Avatar of lhutton

ASKER

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.
Avatar of Jan Louwerens
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?)
lhutton: do you want a solution that capitalizes the first letter of a word, or the first letter of every word in a string?
Avatar of lhutton

ASKER

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.
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...
if you're having trouble capitaling the words, let me know and I'll figure out how to do it...
Avatar of lhutton

ASKER

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???
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...
Avatar of lhutton

ASKER

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???
ok, I'll figure it out and post it here... give me a few minutes, ok?  =)
ok, I'll figure it out and post it here... give me a few minutes, ok?  =)
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)"
Oops!!! The inner loop should be:

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

sorry about that!!
Avatar of lhutton

ASKER

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
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...
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
Avatar of lhutton

ASKER

Works now :-) One more thing... what browsers will support the script and how can I prevent errors/problems in browsers that don't?
Avatar of lhutton

ASKER

One error:
wordArray[word][0] has no properties.
appears when the string is removed from the field and left blank.
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;
Avatar of lhutton

ASKER

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)?
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...
Avatar of lhutton

ASKER

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?
you should just use:
onChange="capitalize(this)"
Avatar of lhutton

ASKER

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.
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.
Avatar of lhutton

ASKER

Thank you for all your help. Much appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lhutton

ASKER

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?
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...
Avatar of lhutton

ASKER

The error being reported is:

Line: 46
Char: 9
Error: '0' is not an object
Code: 0
well, without seeing your code that doesn't really help me... can you post the line it's on?
Avatar of lhutton

ASKER

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.
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();
Avatar of lhutton

ASKER

That puts "UNDEFINEDundefinedundefinedundefined" into the text box.
Avatar of lhutton

ASKER

So, the only other possible lines are:

if (sentence.length > 0)

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

if (word > 0)
ok, well, when it put "UNDEFINDED..." in the text box, did the error go away? Is it running ok now, just giving bad results?
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)">
Avatar of lhutton

ASKER

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?
Avatar of lhutton

ASKER

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...
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...
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.
Avatar of lhutton

ASKER

Thank you very much for your input. It's much appreciated.