Bryuk
Bryuk

Reputation: 3345

How to validate textbox only enter several options in asp.net c#?

In my Database I have a table with currency codes like:

USD
GBP
CAD
AUD
JPY
EUR
ISK
PLZ
TRL ... and more

In my asp.net app I have a textbox for user to enter currency.

<asp:TextBox ID="txtCurrency" runat="server" Text="USD"></asp:TextBox>

How to force user to enter only options that I have in my DB?

Upvotes: 2

Views: 907

Answers (3)

hutchonoid
hutchonoid

Reputation: 33306

If you must you could use a regex validator control:

<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="txtCurrency"
                     ValidationExpression="^(USD|GBP|CAD|AUD|JPY|EUR|ISK|PLZ)$"
                     Display="Static"
                     EnableClientScript="false"
                     ErrorMessage="Enter a valid code"
                     runat="server"/>

This would be populated server-side like this but formatted like the regex above:

RegularExpressionValidator1.ValidationExpression = GetValuesFromDb();

This is how you would format the values into the regex:

    var cs = new List<string> {"USD", "GBP", "CAD", "AUD", "JPY", "EUR", "ISK", "PLZ"};
    var csRegex = "^({0})$";
    Console.Out.Write(string.Format(csRegex, string.Join("|", cs)));

But from a UI experience I feel that a dropdown or UI autocomplete as others have suggested would be better suited.

Upvotes: 1

speti43
speti43

Reputation: 3046

You can implement Autocomplete box easily.

Here is a walkthrough: Creating a Simple Auto-Complete TextBox

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
</asp:ToolkitScriptManager>  

<asp:TextBox ID="txtCurrency" runat="server"></asp:TextBox>  

<asp:AutoCompleteExtender   
    ID="AutoCompleteExtender1"   
    TargetControlID="txtCurrency"   
    runat="server" />  

And create a page method for AutoCompleteExtender(described in walkthrough).

You can use Jquery autocomplete also if you want to make it on client-side, but if you want to retrieve data from DB, the you have to create a webservice method or handler to feed the autocomplete with json data.

I think this more user friendly than a simple validation on free-text textbox. At the end you can use some validation by regex (on client and also server side). You can go with HTML5 validation also via regex: HTML5 validation

Or just use built in asp.net validators, it handles client and server side validation automatically. ASP.Net Validators

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25715

Well, what you could do is use Autocomplete option, and once you have the currency types, you can then selectively consume keystrokes that do not match the currency being typed.

Like for example, if the user has typed U and is trying to type something like M(assuming that no currencies begin with UM) then the keystroke M can be consumed, and only then if the user is trying to type S should it allow (since, there is something called "USD" in the database).

Second option would be something like select2

Upvotes: 0

Related Questions