textbox allow only letters

I need some help with a textbox:

The textbox is only allowed to contain letters.
how do I do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Check here : How to make Textbox only accept alphabetic characters, there are some different approached to choose from.

Edit: I see on the tags that we are talking asp.net, which the question I mentioned discusses the problem from a WinForms perspective. Might be that you want to do something similar using the onKeyPress event in JavaScript instead.

This article on w3schools.com contains an example code that may do exactly what you are looking for.

Method 2

You can use regular expression validation for this and use following regular expression
“^[a-zA-Z]+$”

this is the easiest way to do this.

Method 3

You could do this with javascript to filter out which charaters you accept, but if you can use Asp.net AJAX then you can add a FilteredTextBoxExtender to the textbox and only allow upper case and/or lower case letters.

To add a FilteredTextBoxExtender in Visual Studio 2008 you can do the following:

  • view the page in design mode and find the textbox
  • click the arrow on the right side of the textbox to open the TextBox Tasks menu and select Add Extender.
  • Then select the FilteredTextBoxExtender and click OK
  • Now your textbox should have a new property available in the designer. Make sure the textbox is selected and click F4 to open the properties designer.
  • Find the new property in the property designer. It should be named YOURTEXTBOXNAME_FilteredTextBoxExtender. Find the FilterType property and select UppercaseLetters or LowercaseLetters.
  • If you want both upper case and lower case letters you must edit the markup directly and set its value to “UppercaseLetters, LowercaseLetters”

Method 4

Javascript/JQuery is your friend to make the UI only accept letters. However, you must make sure you validate the contents of the textbox on postback otherwise you can have bad data where people have bypassed the javascript.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x