Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how validate textbox that should accept character not the numeric values.
pls any one having idea about this pls share.
Posted

Try this:
C#
void CharacterValidation(object sender, KeyPressEventArgs e)
       {
           if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || e.KeyChar == 32 || e.KeyChar == 8)
           {
               e.Handled = false;
           }
           else
           {
               e.Handled = true;
           }
       }
 
Share this answer
 
Comments
__TR__ 4-Sep-12 4:51am    
5ed!
Prasad_Kulkarni 4-Sep-12 4:54am    
Thank you TR!
D-Kishore 4-Sep-12 5:07am    
Mine 5
Prasad_Kulkarni 4-Sep-12 5:22am    
Thank you Kishore!
ridoy 4-Sep-12 5:44am    
good..+5
You can also do using regex in the button click event like below

C#
bool Valid = false;

C#
private void button1_Click(object sender, EventArgs e)
        {

            Regex rx = new Regex("^[a-zA-Z]*$");
            if (!string.IsNullOrEmpty(TxtProjectName.Text))
            {
                if (rx.IsMatch(TxtProjectName.Text))
                {
                    Valid = true;
                }
                else
                {
                    MessageBox.Show("Numbers are not allowed.");
                    TxtProjectName.Clear();
                    TxtProjectName.Focus();
                    return;
                }
            }
            else
            {
                MessageBox.Show("Please enter a project name and click Ok.");
                TxtProjectName.Focus();
                //this.ControlBox = true;
                return;
            }
        }
 
Share this answer
 
Comments
ridoy 4-Sep-12 5:44am    
Regex is always a good and easy solution..so +5
D-Kishore 4-Sep-12 6:02am    
Thanks Ridoy
Mohamed Mitwalli 4-Sep-12 8:24am    
5+
Prasad_Kulkarni 4-Sep-12 8:26am    
5'ed!
Try This Link

Click Here

You can use FilteredTextBox Property of the AjaxToolKit.

Example:
VB
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
    TargetControlID="TextBox3"
    FilterType="Custom, Numbers"
    ValidChars="+-=/*()." />
 
Share this answer
 
v3
Comments
SamWakchaure 4-Sep-12 4:50am    
i want to validatee for windows forms
sunandandutt 4-Sep-12 4:52am    
You can try Parshad_kulkarni Answer for Windows.

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