Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We want to insert only character in text box not number.

How is this possible ? Please tell me.
Posted
Updated 17-Jun-11 1:54am
v2
Comments
That's Aragon 17-Jun-11 7:55am    
Edited for better readability.

use that RegularExpressionValidator

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1" ValidationExpression="[aA-zZ]"></asp:RegularExpressionValidator> 
 
Share this answer
 
Comments
NDebata 17-Jun-11 6:20am    
Best way, but in this case first you allowing to enter a number, then showing a validation error.
nit_singh 17-Jun-11 13:55pm    
yes, like a normal validation control
Monjurul Habib 17-Jun-11 16:56pm    
nice answer, my 5. Please have a look at my answer.
It's a web app, according to your tags, so you need to write some JavaScript validation code. 'onchange' is the event in the DOM that corresponds to TextChanged. You should also handle 'onkeypress' and reject numbers, because modifying the text in onchange causes the caret position to be reset and that's annoying if it happens every time you enter an invalid character.
 
Share this answer
 
Comments
Monjurul Habib 17-Jun-11 16:57pm    
nice answer, my 5. Please look at my answer.
You can also do this using simple javascript:
C#
function AllowAlphabet(e)
{
  isIE = document.all ? 1 : 0
  keyEntry = !isIE ? e.which : event.keyCode;
  if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
     return true;
  else
{
    alert('Please Enter Only Character values.');
    return false;
      }
}

<asp:textbox id="TextBox1" runat="server" maxlength="1" onkeypress="return AllowAlphabet(event)" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 
Comments
thatraja 17-Jun-11 21:53pm    
Yep, Good solution, 5!
Monjurul Habib 18-Jun-11 3:28am    
Thank you.
There are a couple of ways, but the most reliable is to handle the TextChanged Event and screen the entire text. This prevents pasting digits as well, which the KeyPress Event doesn't.
private void myTextBox_TextChanged(object sender, EventArgs e)
    {
    Regex r = new Regex(@"\d");
    if (r.IsMatch(myTextBox.Text))
        {
        int i = myTextBox.SelectionStart;
        myTextBox.Text = r.Replace(myTextBox.Text, "");
        myTextBox.SelectionStart = i;
        }
    }
(the SelectionStart stuff is to preserve the caret position)
 
Share this answer
 
Comments
BobJanova 17-Jun-11 6:22am    
It's tagged ASP.net, so I think he wants a web solution.

E: the 1 vote was not me, if you are wondering.
thatraja 17-Jun-11 7:45am    
Poor Griff, have your coffee now.
Monjurul Habib 17-Jun-11 16:58pm    
Please look at my answer.
 
Share this answer
 
v5
hello friend try this code

ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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