How Can I Force Input To Uppercase In An Asp.net Textbox?
I'm writing an ASP.NET application. I have a textbox on a webform, and I want to force whatever the user types to upper case. I'd like to do this on the front end. You should also
Solution 1:
Why not use a combination of the CSS and backend? Use:
style='text-transform:uppercase'
on the TextBox, and in your codebehind use:
Textbox.Value.ToUpper();
You can also easily change your regex on the validator to use lowercase and uppercase letters. That's probably the easier solution than forcing uppercase on them.
Solution 2:
Use a CSS style on the text box. Your CSS should be something like this:
.uppercase
{
text-transform: uppercase;
}
<asp:TextBox ID="TextBox1" runat="server" Text="" CssClass="uppercase"></asp:TextBox>;
Solution 3:
Okay, after testing, here is a better, cleaner solution.
$('#FirstName').bind('keyup', function () {
// Get the current value of the contents within the text boxvar val = $('#FirstName').val().toUpperCase();
// Reset the current value to the Upper Case Value
$('#FirstName').val(val);
});
Solution 4:
**I would dolike:
<asp:TextBoxID="txtName"onkeyup="this.value=this.value.toUpperCase()"runat="server"></asp:TextBox>**
Solution 5:
You can intercept the key press events, cancel the lowercase ones, and append their uppercase versions to the input:
window.onload = function () {
var input = document.getElementById("test");
input.onkeypress = function () {
// So that things work both on Firefox and Internet Explorer.var evt = arguments[0] || event;
var char = String.fromCharCode(evt.which || evt.keyCode);
// Is it a lowercase character?if (/[a-z]/.test(char)) {
// Append its uppercase version
input.value += char.toUpperCase();
// Cancel the original event
evt.cancelBubble = true;
returnfalse;
}
}
};
This works in both Firefox and Internet Explorer. You can see it in action here.
Post a Comment for "How Can I Force Input To Uppercase In An Asp.net Textbox?"