Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

i want to take the first character as capital automatically in textbox
Posted

If you wanted it through javascript, then try this
XML
<script type="text/javascript" language="javascript">
    function capFirst(oTextBox) {
        oTextBox.value = oTextBox.value[0].toUpperCase() + oTextBox.value.substring(1);
               }
</script>
<asp:TextBox runat="server" onChange="javascript:capFirst(this);" ID="Tb1" AutoPostBack="False" />
 
Share this answer
 
Comments
[no name] 26-Sep-12 3:32am    
Have u got the solution???
Santhosh Kumar Jayaraman 26-Sep-12 3:33am    
You are asking me?
[no name] 26-Sep-12 3:34am    
I'm asking pradeep...
[no name] 26-Sep-12 3:34am    
If not got the solution,
private void tb1_Leave(object sender, EventArgs e)
{
tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
}
Use the above coding u ll get the answer...
C#
private void tb1_TextChanged(object sender, EventArgs e)
        {
         tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
            }
 
Share this answer
 
In case if use winforms

C#
private void tb1_Leave(object sender, EventArgs e)
        {
         tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
            }


Incase of aspnet
C#
private void tb1_TextChanged(object sender, EventArgs e)
        {
         tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);
            }


Dont forget to use Autopostback=true for the textbox in aspx
 
Share this answer
 
v4
Comments
Santhosh Kumar Jayaraman 26-Sep-12 2:41am    
that is an event comes with each control.Check this http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx
Pradeep_kaushik 26-Sep-12 2:52am    
this event is not shown in my event window
Santhosh Kumar Jayaraman 26-Sep-12 2:54am    
OK sorry.I didnt see you were using aspnet.. Use textchanged event and write the same method
tb1.Text = tb1.Text.Substring(0, 1).ToUpper() + tb1.Text.Substring(1);

textbox_leave event is for winforms not for aspnet.
Pradeep_kaushik 26-Sep-12 2:55am    
ok thanks
Santhosh Kumar Jayaraman 26-Sep-12 3:22am    
Dont forget to use Autopostback=true for the textbox in aspx
The simplest way to do this would be when you submit the form. If you really want to do it as the user enters it, you can use the onkeyup event handler to change the data, but it's going to be more work to make sure that the cursor stays where the user put it (especially if they type in the middle of an existing word).

The Javascript logic for capitalization like this is pretty simple:

JavaScript
function capFirst(oTextBox) {
    var sText = oTextBox.value;
    sText = sText.substring(0,1).toUpperCase() + sText.substring(1,sText.length);
    oTextBox.value = sText;
    }
 
Share this answer
 
string myString = "tHis IS my tEXT";

TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
myTI.ToTitleCase(myString);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900