Click here to Skip to main content
14,732,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The text box enter only characters in wpf.....................
Posted
Comments
Maciej Los 2-Nov-12 3:10am
   
Dibin Babu, if you have any question or comment to existing solution, please, use "Have a wuestion or comment" widget instead of "Submit your solution".

<TextBox Name="txtProductCode">
  <TextBox.Text>
    <Binding Path="ProductCode">
      <Binding.ValidationRules>
        <jas:RegexValidationRule

          RegexText="^[A-Za-z]*$"

          ErrorMessage="Enter only character"

          RegexOptions="IgnoreCase"

          />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>
   
Comments
Maciej Los 2-Nov-12 3:07am
   
Comment from OP:


jas shows an error.........jas is an undeclared prefix...
   
Try this simple code...



private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
//allows only character
            if (char.IsChar(e.KeyChar) == true)
            {
                e.Handled = true;
            }
//backspace working code
            if (e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
            }
        }
   
v2
Comments
Maciej Los 2-Nov-12 3:07am
   
Comment from OP:

keyPress events are not present in text box.........................

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