All the data in the system must be save as uppercase, with some exceptions (passwords, emails, login...), so I create a css to do the visual trick
input[type="text"], textarea { text-transform:uppercase; }
.small_case { text-transform:smallcase !important; }
using the small_case
class to the fields that are exception (the passwords doesn't need this).
But as you all know, this will keep the value on the server-side
as the user input it.
Example:
- input: Abcd
- visual: ABCD
- server: Abcd
So I did a jQuery function to "fix"
the problem:
$(document).ready(function () {
$("input, textarea").on('keypress blur', function () {
if ($(this).hasClass('small_case')) {
$(this).val($(this).val().toLowerCase());
}
else {
$(this).val($(this).val().toUpperCase());
}
});
});
PS: Why am I using keypress
AND blur
?
- right-click and paste a value into an input
- press enter-key to save or do a search
- result: the value will be as the user input. not as uppercase
BUT, I HAVE A SMALL PROBLEM
In some inputs I use a mask component, that creates the basic structure
Example:
- value input: ABC1234
- mask input: _ _ _ _ _ _ _
- while input: AB_ _ _ _ _ ; ABC12_ _ ;
Using the jQuery code above, the Caret will be positioned at the end
Example:
- A_ _ _ _ _ _| (where | is my caret cursor)
What I was trying to do, but not sure how or if this is the right/best solution?
- Find via regex what's the index of the last character with the same keycode as the character that I last typed and set the carat to that position.
- Using only blur, will avoid the problem, but I fall under the PS below the jQuery code.
So... any advice?