简体   繁体   中英

textbox allow only letters

I need some help with a textbox:

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

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.

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

this is the easiest way to do this.

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM