How could I force the text in the \"username\" text input
to be lower-case regardless of what user types?
Using jquery assuming that the input ID is username
$(document).ready(function(){
$("#username").on('change keyup paste',function(){
$(this).val($(this).val().toLowerCase());
})
})
This is my suggestion, it's based on the answer from @fdiv-bug & @ali-sheikhpour:
input[type="email"] {
text-transform: lowercase;
}
var upperCaseMatch = /[A-Z]/;
var events = {
CHANGE: 'change'
};
$(function() {
$(document).on('change', 'input[type="email"]', function() {
var value = $(this).val();
if (!upperCaseMatch.test(value)) {
return;
}
$(this).val(value.toLowerCase());
});
});
Hope its useful for you.
I use this simple code :
<input type="text" onkeyup="this.value = this.value.toUpperCase();">
in CSS:
form input[type="text"] {
text-transform: lowercase;
}
otherwise in JS:
var text="this is my text.";
var lowercase=text.toLowerCase();
Combining a bit of everyone's answer to here simplify things.
Use CSS to avoid any flashing and for display purposes.
input[type="username"] {
text-transform: lowercase;
}
Now, because this ONLY effects DISPLAY of the text in the browser, we need to also change the value of the input.
Add an event listener to the input.
const usernameInput = document.querySelector('input[type="username"]');
usernameInput.addEventListener("input", function(e){
e.target.value = e.target.value.toLowerCase();
});
We can send this to the sever like normal and, like others have mentioned, check server-side to make sure a malicious user didn't send us UpPPercaSe input.
Using jquery assuming that the input ID is username:
$(document).ready(function(){
$("#username").on('input', function(){
$(this).val( $(this).val().toLowerCase() );
})
});