Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make everything lowercase automatically in Javascript as they type it in

Tags:

javascript

How do I make all of the characters of a text box lowercase as the user types them into a text field in Javascript?

<input type="text" name="thishastobelowercase">

Thanks for any help.

like image 568
Jordash Avatar asked Aug 09 '11 16:08

Jordash


People also ask

How do you make everything lowercase in JavaScript?

JavaScript String toLowerCase() The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.

How do you change input to lowercase?

In Python, lower() is a built-in method used for string handling. The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.

Which method is used to lowercase all the characters in a string in JavaScript?

The toLowerCase() method returns the value of the string converted to lower case. toLowerCase() does not affect the value of the string str itself.


4 Answers

I would just make CSS do this for you instead of monkeying around with javascript:

<input type="text" name="tobelowercase" style="text-transform: lowercase;">
like image 113
Justin Beckwith Avatar answered Oct 06 '22 05:10

Justin Beckwith


Two ways:

Using CSS:

.lower {
   text-transform: lowercase;
}

<input type="text" name="thishastobelowercase" class="lower">

Using JS:

<input type="text" name="thishastobelowercase" onkeypress="this.value = this.value.toLowerCase();">
like image 29
Mrchief Avatar answered Oct 06 '22 05:10

Mrchief


$('input').keyup(function(){
    this.value = this.value.toLowerCase();
});
like image 36
bjornd Avatar answered Oct 06 '22 04:10

bjornd


Does it only have to display in lowercase, or does it have to be lowercase? If you want to display lowercase, you can use CSS text-transform: lowercase.

You need to enforce this constraint server-side anyway, because the user can disable any JS code you put in to enforce that it remains lowercase.

My suggestion: use the CSS text-transform to make it always display in lowercase, and then do a toLower or your language's variant of it on the server-side before you use it.

like image 38
BishopRook Avatar answered Oct 06 '22 06:10

BishopRook