views:

12387

answers:

13

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 note that there is a validation control on this textbox, so I want to make sure the solution doesn't interfere with the asp.net validation.

Clarification: It appears that the CSS text-tranform makes the user input appear in uppercase, however under the hood, it's still lower case as the validation control fails. You see, my validation control checks to see if a valid state code is entered, however the regex expression I"m using only works with uppercase characters.

+5  A: 

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>
billb
The REGEX validator will still fail with a purely CSS solution.
Stephen Wrighton
A: 

Set the style on the textbox as text-transform: uppercase?

Will
A: 

Javascript has the "toUpperCase()" function of a string.

So, something along these lines:

function makeUpperCase(this)
{
this.value = this.value.toUpperCase();
}
Stephen Wrighton
That will only change it after, no as they type it.
Diodeus
You could append this function to the onchange event of the text area and it would update it on the fly, though it would be a little bit slow. CSS is the way to go IMHO.
Tom
@diodeus - if you use TextChange event then it'll occur as they top. @Tom - does the CSS actually change the underlying ASCI or just how it's displayed?
Stephen Wrighton
@Stephen - That's a really good question! I created a small page to test this out and it appears that CSS only changes the way that it appears _not_ the underlying ASCII.
Tom
@Tom - That's what I thought, as such, it needs to occur via JS rather than CSS in order for the validator to fire correctly
Stephen Wrighton
A: 
 style='text-transform:uppercase'
Diodeus
A: 

CSS could be of help here.

style="text-transform: uppercase";"

does this help?

shahkalpesh
A: 

Use the text-transform CSS for the front-end and then use the toUpper method on your string server-side before you validate.

The validation control that's failing runs client-side.
Aheho
+1  A: 

I just did something similar today. Here is the modified version:

<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
<script type="text/javascript">
    function setFormat() {
        var inp = document.getElementById('ctl00_MainContent_txtInput');
        var x = inp.value;
        inp.value = x.toUpperCase();
    }

    var inp = document.getElementById('ctl00_MainContent_txtInput');
    inp.onblur = function(evt) {
        setFormat();
    };
</script>

Basically, the script attaches an event that fires when the text box loses focus.

Jason Z
I don't get this. Where did you get the id 'ctl00_MainContent_txtInput' from?
Aheho
ASP.NET translates your IDs. You can figure them out by viewing the source of the page you are working with. ctl00 is the generic prefix (but it can be different), MainContent just happened to be the name of the Form that the control was placed in.
Jason Z
Couldn't this potentially break when moving to a newer version of ASP.NET?
Aheho
Possibly, but you could always use a unique text box name and a JavaScript framework like jQuery and find the control where the unique name matches a pattern. I was trying to keep the sample code simple.
Jason Z
There's a better way to get the client id -> <%= TextBox1.ClientID %>. That would render the correct ID.
Cyril Gupta
+12  A: 

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.

Rorschach
I'd go with ToUpper(). Why would you want to enforce such restrictions on the end-user? That's not a friendly UI. Take whatever the user gives you and make it upper case yourself.
Kon
I would do this too. Messing around in Javascript until you have what you want is much more work, and you require Javascript to be enabled which is simply unnecessary with Rorschach's solution.
Rob
+5  A: 

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 FF and IE
        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;
            return false;
        }
    }
};

This works in both FF & IE. You can see it in action here.

Ates Goral
But it doesn't work if the user tries to type in the middle of the text.
erikkallen
+1  A: 
<!-- Script by hscripts.com -->
<script language=javascript>
function upper(ustr)
{

    var str=ustr.value;
    ustr.value=str.toUpperCase();
}

function lower(ustr)
{

    var str=ustr.value;
    ustr.value=str.toLowerCase();
}


</script> 
<form>
Type Lower-case Letters<textarea name="address" onkeyup="upper(this)"></textarea>
</form>

<form>
Type Upper-case Letters<textarea name="address" onkeyup="lower(this)"></textarea>
</form>
+2  A: 

I would do this using jQuery.

<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#txt").keydown(function(e) {
            if (e.keyCode >= 65 & e.keyCode <= 90) {
                val1 = $("#txt").val();
                $("#txt").val(val1 + String.fromCharCode(e.keyCode));
                return false;
            }
        });
    });
</script>

You must have the jquery library in the /script folder.

Cyril Gupta
This version works as the user types.
Cyril Gupta
+1  A: 
**I would do like:
<asp:TextBox ID="txtName" onkeyup="this.value=this.value.toUpperCase()" runat="server"></asp:TextBox>**
Vinay Yadav
A: 

I realize it is a bit late, but I couldn't find a good answer that worked with ASP.NET AJAX, so I fixed the code above:

function ToUpper() {
        // So that things work both on FF and IE
        var evt = arguments[0] || event;
        var char = String.fromCharCode(evt.which || evt.keyCode);

        // Is it a lowercase character?
        if (/[a-z]/.test(char)) {
            // convert to uppercase version
            if (evt.which) {
                evt.which = char.toUpperCase().charCodeAt(0);
            }
            else {
                evt.keyCode = char.toUpperCase().charCodeAt(0);
            }
        }

        return true;
    }

Used like so:

       <asp:TextBox ID="txtAddManager" onKeyPress="ToUpper()" runat="server" 
             Width="84px" Font-Names="Courier New"></asp:TextBox>
NetMage