tags:

views:

1459

answers:

4

Hello I need some help with a textbox.

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

Greetings! and Thanks in advance

+2  A: 

Check here : http://stackoverflow.com/questions/790378/how-do-i-make-textbox-to-only-accept-alphabets-appear-error-if-is-not-an-alphabet/, 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.

Fredrik Mörk
A: 

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"
Rune Grimstad
This Seem to Work Great after installing the ajax toolkid :)
Entvex
+1  A: 

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.

Robin Day
In principle I'd agree, but asp.net makes this *much* harder than it needs to be, especially in 2.0. The fastest and least painful approach is just using built in regex validation controls.
annakata
+1  A: 

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

this is the easiest way to do this.

jalpesh