3

I want to allow only English characters to be typed in my asp:TextBox (in asp:Login control).

How can I do that?

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Which of the following would you like to count as 'English characters': é as in lamé, ç as in soupçon, ï as in naïve, ô as in rôle... – AakashM Mar 22 '10 at 15:46
  • @AakashM: I don't want user to type 'Вася Пупкин' in login or password fields :) – abatishchev Mar 22 '10 at 15:49

2 Answers2

12

If you want to validate the input

Use a RegularExpressionValidator control. There is a decent example and explanation at How To: Use Regular Expressions to Constrain Input in ASP.NET.

The regular expression for only English (upper and lower case A-Z) characters is ^[A-Za-z]*$

If you want to include numbers too, ^[A-Za-z0-9]*$

Snippet of the markup you'll want to add:

<asp:RegularExpressionValidator ID="regexpName" runat="server"     
      ErrorMessage="Only upper and lower case A-Z may be entered here." 
      ControlToValidate="txtInput"     
      ValidationExpression="^[A-Za-z]*$" />

If you want to prevent a user from typing anything else into the box

You're better off using the jQuery script in the answer to this question: how do i block or restrict special characters from input fields with jquery?

BUT you are always going to want to validate the input too, (what if the user turns off JavaScript for example), so use the validation method described above as well.

Community
  • 1
  • 1
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
1

You can achieve this using the FilteredTextBox.

It can also be done with plain javascript (FilteredTextBox also relies on javascript, but you can do it with only javascript that you write yourself) by registering an onKeyUp listener, and filtering out the characters that are not in an allowed subset (perhaps with a regex replace)

David Hedlund
  • 128,221
  • 31
  • 203
  • 222