Allow only alpha characters into textbox in asp.net - c#

How can I prevent the user from entering anything but alpha characters in my textbox?
Update
Forgot to mention that its a dynmic control (created on the form when the user clicks a button).

With built-in ASP.NET features only, you could use the <asp:RegularExpressionValidator> control, with a regular expresion like [A-Za-z]*. This will validate on server side (which should be your main concern), and give a user-friendly error message on postback. There are ways to use these controls for clientside validation as well if you're using the MVC framework, but I don't know how or how well that works in WebForms.
Using jQuery, there are endless possibilities for clientside validation as well with the jquery.validation plugin.

The AjaxControlToolkit has a FilteredTextBox extender that allows you to specify a combination of both lower- and uppercase letters.

I would set a CssClass on the dynamically created textbox, then use jQuery and some custom javascript/regex to filter any input field with that CssClass on the keyup event.
The filtered TextBox extender is good, and you could probably use that as well, for me, the jQuery solution would be a little easier and flexible.

Related

Textboxes in Asp.Net using C#?

How to get different colors of text in a text box in ASP.NET when words printed in the Text Box are dynamic using database? Please help! There is rich text box for this purpose in Window Form but what about the web forms?
Standard ASP.NET TextBox renders as <input type="text"> which does not support styles for individual characters.
You can use a 3rd party tool or, at a very basic you can use DIV with contenteditable set to true:
Here's a small demo: http://jsfiddle.net/Pr9mm/
If you are looking for a rich-text box to use - try the HTMLEditor in the AjaxControlToolkit. It is essentially the same functionality. Just make sure to put a sanitizer on it to avoid XSS problems.
Check the accepted answer for this question. Eventhough it is for gridview the same logic could be used for textbox also. Hope this helps you in right direction.
Consider using the HtmlEditorExtender from the ajax control toolkit. You can also configure how the textbox looks.

Yet another ASP:Textbox vs <input type="text">

I have read through some articles on this topic but I am still cautious about this. I am all along using ASP:Textbox but I would like to know what are the things an input textbox cannot possibly perform without using a ASP:Textbox or takes much more effort to pull off?
I have a Jquery tooltip sample which uses HTML input textbox and I am not sure if I should change all my ASP:Textboxes to HTML textboxes, the things which I need to perform on this textboxes are RequiredFieldValidation as well as storing their values into the database.
Anyone can advice me on this rookie question. Thanks.
The asp.net textbox IS a html input box from jQuerys point of view. Everything special about it is done on the server (including viewstate validation). The question you need to ask, is what value does giving it a server side reference bring to your app? Generally the answer is easy server side reference, but does that apply in your case?
As far as i know there is no visible server side programming difference between a HTML input box and an ASP Textbox. ASP.NET Validators would work perfectly both ways.
As for your tooltip concern. Jquery doesn't care if your using an ASP Textbox since your ASP textbox will end up as an HTML textbox anyways.
And for the advice - i don't recommend you changing all your ASP Textbox to HTML input boxes, its just a waste of time. You should use the CSSClass / class property to display the tooltip instead. That way it would work on your regular HTML input box and ASP Textboxes + other page elements.

Is there a way to NOT load a specific control in ASP.NET/C#?

I've a container (RadDockZone - Telerik), and inside it, a textbox field with an "Asp:RequiredFieldValidator", which makes it mandatory. Every time that I work with AJAX operations, the entire container is "reloaded", including the RequiredFieldValidator. This makes the ValidationSummary show the same error message twice, until the page is completely reloaded (I'm using RadAjaxManager to solve the AJAX operations).
So, how can I "don't reload" a specific control (in this case, the RequiredFieldValidator) using C#?
Regards!
If you place the following in page load if(page.Isvalid) the required field validator will be fired once after submitting that form. The message will not be displayed twice.
If you do not include the textbox (or its wrapping containers) in the list of controls updated by the ajax manager, the textbox and the respective required validator should not be updated on ajax requests and the error message should not be visualized.
You should use validation groups. This lets you group all of your validation controls into a group. Then put this same validation group on your submit button and those validation controls will only validate when that particular button is clicked.
Property is assigned on the validation control and button using the ValidationGroup property.
Thomasvdb's suggestion of setting CausesValidation to false also might be a good way to do it.

Validation in Silverlight

how to validate the input form of a silverlight control?
I have 3 controls, two out of three are text boxes (For name, and Age), and the remaining one control is date picker.
When i hit submit button, the validation should be invoked. how it will done??
thanks in advance.
If you're using Silverlight 3 check out Data Validation. http://www.silverlightshow.net/items/Data-Validation-in-Silverlight-3.aspx
If you're using Silverlight 2 you'll have to roll your own code for validation.
I wrote my own validaion for SL2. It's based on:
Attached property to give control custom validation ID
Business object validators that identify invalid data
VisualTreeHelper to parse visual tree and match validation result and custom validation ID
Custom templates for controls in order to display validation
INotifyPropertyChanged to remove validation display if property value was changed.
You may also find the Validation Application Block (which is part of the Enterprise Library Silverlight Integration Pack) useful. It's in public preview right now.

Safe HTML in ASP.NET Controls

Im sure this is a common question...
I want the user to be able to enter and format a description.
Right now I have a multiline textbox that they can enter plain text into. It would be nice if they could do a little html formatting. Is this something I am going to have to handle? Parse out the input and only validate if there are "safe" tags like <ul><li><b> etc?
I am saving this description in an SQL db. In order to display this HTML properly do I need to use a literal on the page and just dump it in the proper area or is there a better control for what I am doing?
Also, is there a free control like the one on SO for user input/minor editing?
Have a look at the AntiXSS library. The current release (3.1) has a method called GetSafeHtmlFragment, which can be used to do the kind of parsing you're talking about.
A Literal is probably the correct control for outputting this HTML, as the Literal just outputs what's put into it and lets the browser render any HTML. Labels will output all the markup including tags.
The AJax Control Toolkit has a text editor.
Also, is there a free control like the
one on SO for user input/minor
editing?
Stackoverflow uses the WMD control and markdown as explained here:
https://blog.stackoverflow.com/2008/09/what-was-stack-overflow-built-with/
You will need to check what tags are entered to avoid Cross side scripting attacks etc. You could use a regex to check that any tags are on a 'whitelist' you have and strip out any others.
You can check out this link for a list of rich text editors.
In addition to the other answers, you will need to set ValidateRequest="false" in the #Page directive of the page that contains the textbox. This turns off the standard ASP.NET validation that prevents HTML from being posted from a textbox. You should then use your own validation routine, such as the one #PhilPursglove mentions.

Categories