1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Upper and Lowercase Conversion

clr gif
Join the Discussion

Questions? Comments?

When your visitors type something into forms on your web page they can select for themselves whether to use uppercase or lowercase and in what combination. Reasonable possibilities for input are all lowercase, all uppercase, or with the first letter of some or all words in uppercase with the rest in lowercase.

It can be difficult for subsequent processing where the case used varies from one input to the next. The best solution is to perform some manipulations on the date on the server when the form is processed to convert the case of the input to a standard one. This gives us consistant capitalization in what we process on the server but doesn't help when it comes to any Javascript validations done on the field before the form is submitted to the server.

To resolve this we need to be able to convert what has been input to a consistent format for processing through the validation routine. The simplest way to get the validation to disregard the case used is to convert everything to either all uppercase or all lowercase. This can be easily done with one statement. To convert a text string (let's call it 'myfield') to all uppercase we would use:

myfield = myfield.toUpperCase();

Similarly to convert to all lowercase we use:

myfield = myfield.toLowerCase();

Making the first letter of each word uppercase with the rest in lowercase is slightly more complicated. There is no string method to adjust the case of the text like that so we'll have to add one ourselves. Here is code that you can use to add the new method.

String.prototype.toCapitalCase = function() {
var re = /\s/;
var words = this.split(re);
re = /(\S)(\S+)/;
for (i = words.length - 1; i >= 0; i--) {
words[i] = RegExp.$1.toUpperCase()
+ RegExp.$2.toLowerCase();
return words.join(' ');

Note that this code also reduces all whitespace between words into single spaces but for the sorts of text strings where you will want to apply this conversion I don't expect that to be a problem.

With that defined before we use it we can convert the first letter of each word to uppercase and the rest of each word to lowercase using:

myfield = myfield.toCapitalCase();
See More About

©2017 About.com. All rights reserved.