textbox allow only letters

前端 未结 4 1085
余生分开走
余生分开走 2021-01-25 01:42

I need some help with a textbox:

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

相关标签:
4条回答
  • 2021-01-25 02:41

    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"
    0 讨论(0)
  • 2021-01-25 02:41

    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.

    0 讨论(0)
  • 2021-01-25 02:42

    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.

    0 讨论(0)
  • 2021-01-25 02:46

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

    this is the easiest way to do this.

    0 讨论(0)
提交回复
热议问题