Click here to Skip to main content
11,924,800 members (56,040 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET validation
how to restrict user not to enter other numbers in textbox except 12.

i have a textbox on my webpage the user should enter only the number 12 in this textbox.

if he enters other number, it must shows a error .

Please give me good solution. by the way am working on c#.

Hlep me ! thank you in advance
Posted 18-Jan-13 21:34pm
deepakaitr12345 22-Jan-13 8:28am
Hi @Ranjith..

For more specifically you can use filtertextbox extender to restrict the other no to be entered.Or you can use javascript to check the values for the text boxes.

Hope this helps you.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The easiest would be to handle the keydown[^] event and only allow 12 to be entered.

Some other articles that might provide some insight -[^][^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can restrict the user to enter any characters other than numbers or any particular set of characters. Check out this article. it talks about a technique to so the similar thing.

A Tiny Javascript Framework for Common Validation Scenarios.[^]

it would be something like

<asp:TextBox ID="TextBox7" runat="server" onkeypress="return AcceptRegExOnly(event, /^(1|2)$/);" MaxLength="2"></asp:TextBox>

using this framework. the only catch here is that 11 and 22 are also accepted but that can be circumvented by using a proper regex.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151125.3 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100