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.
JavaScript String toLowerCase() The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.
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.
The toLowerCase() method returns the value of the string converted to lower case. toLowerCase() does not affect the value of the string str itself.
I would just make CSS do this for you instead of monkeying around with javascript:
<input type="text" name="tobelowercase" style="text-transform: lowercase;">
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();">
$('input').keyup(function(){
this.value = this.value.toLowerCase();
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With