views:

7118

answers:

5

How can I convert a JavaScript string value to be in all lower case letters?

Example: "Your Name" to "your name".

A: 

Yes, any string in JavaScript has a toLowerCase() method that will return a new string that is the old string in all lower case. The old string will remain unchanged.

So, you can do something like:

"Foo".toLowerCase();
document.getElementById('myField').value.toLowerCase();
pkaeding
+26  A: 
"Your Name".toLowerCase();
John Topley
+11  A: 

Use either toLowerCase or toLocaleLowerCase methods of the String object. The difference is that toLocaleLowerCase will take current locale of the user/host into account. As per § 15.5.4.17 of the ECMAScript Language Specification (ECMA-262), toLocaleLowerCase

…works exactly the same as toLowerCase except that its result is intended to yield the correct result for the host environment’s current locale, rather than a locale-independent result. There will only be a difference in the few cases (such as Turkish) where the rules for that language conflict with the regular Unicode case mappings.

Example:

var lower = 'Your Name'.toLowerCase();

Also note that the toLowerCase and toLocaleLowerCase functions are implemented to work generically on any value type. Therefore you can invoke these functions even on non-String objects. Doing so will imply automatic conversion to a string value prior to changing the case of each character in the resulting string value. For example, you can apply toLowerCase directly on a date like this:

var lower = String.prototype.toLowerCase.apply(new Date());

and which is effectively equivalent to:

var lower = new Date().toString().toLowerCase();

The second form is generally preferred for its simplicity and readability, but the first has the benefit that it can work with a null value as well while the second requires a string. The result of applying toLowerCase or toLocaleLowerCase on null is null (and not an error condition). The first form may therefore come handy in some generic-handling code.

Atif Aziz
+1  A: 

toLocaleUpperCase() or lower case functions don't behave like they should do. For example in Safari 4, Chrome 4 Beta, Firefox 3.5.x it converts strings with Turkish characters incorrectly. The browsers respond to navigator.language as "en-US", "tr", "en-US" respectively. But there is no way to get user's Accept-Lang setting in the browser as far as I could found. Only Chrome gives me tr although I have configured every browser as tr-TR locale preferred. I think these settings only affect HTTP header, but we can't access to these settings via JS. In the Mozilla documentation it says "The characters within a string are converted to ... while respecting the current locale. For most languages, this will return the same as ...". I think it's valid for Turkish, it doesn't differ it's configured as en or tr. In Turkish it should convert "DİNÇ" to "dinç" and "DINÇ" to "dınç" or vice-versa.

sanilunlu
`Accept-Language` and `navigator.language` are two completely separate settings. `Accept-Language` reflects the user's chosen preferences for what languages they want to receive in web pages (and this setting is unfortuately inaccessible to JS). `navigator.language` merely reflects which localisation of the web browser was installed, and should generally not be used for anything. Both of these values are unrelated to the system locale, which is the bit that decides what `toLocaleLowerCase()` will do; that's an OS-level setting out of scope of the browser's prefs.
bobince
A: 

example

<script type="text/javascript">
var yourstring = 'Your Name'
var lowercase = yourstring.toLowerCase();
document.write('original string:<b> ' + yourstring + '</b><br>');
document.write('converted sting <b>' + lowercase + '</b>');
</script>

try it on

http://htmledit.squarefree.com/
ewwink