Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to give only letters in textbox in asp.net with c sharp without using javascript
Posted
Comments
Sergey Alexandrovich Kryukov 10-Feb-12 5:18am    
The question is a pure abuse. I can explain why. This is asking "do this, but without that". Why, may I ask you? At least explain your motivation. If you need help, explain your goal, your requirements, expected behavior and your rationale, always explain why.
--SA

This Tutorial From Code Project Will Help U Solve ur problem

The 30 Minute Regex Tutorial[^]
 
Share this answer
 
Comments
Aniket Yadav 10-Feb-12 5:39am    
Accept The Answer If It Has Helped You. Doing So Will Boost The Confidence Of The Members Who Is Helping Others.
Hi,
Try this-

We can use Regular expression validator for this:
In the validation expression property keep- "^\d+$."
XML
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 259px; position: absolute;
top: 283px" ValidationGroup="check"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" Style="z-index: 101; left: 424px; position: absolute;
top: 285px" ValidationExpression="^\d+$" ValidationGroup="check"></asp:RegularExpressionValidator>



Javed
 
Share this answer
 
v2
Comments
Rajeev Jayaram 10-Feb-12 5:53am    
Isn't your solution validating numbers and not letters? OP asked for letters. Never mind, atleast she can do that stuff herself.
if regular expressions can be used by you then use them else you can use this piece of code:
C#
string oldText = string.Empty;
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if (textBox2.Text.All(chr => char.IsLetter(chr)))
        {
            oldText = textBox2.Text;
            textBox2.Text = oldText;

            textBox2.BackColor = System.Drawing.Color.White;
            textBox2.ForeColor = System.Drawing.Color.Black;
        }
        else
        {
            textBox2.Text = oldText;
            textBox2.BackColor = System.Drawing.Color.Red;
            textBox2.ForeColor = System.Drawing.Color.White;
        }
        textBox2.SelectionStart = textBox2.Text.Length;
    }


It will make the text box blink on bad input. Please note that it also seems to support paste operations as well.
 
Share this answer
 
v3

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