Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 asp.net c#.

Hlep me ! thank you in advance
Posted
Comments
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.


Thanks

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

XML
<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.
 
Share this answer
 
The easiest would be to handle the keydown[^] event and only allow 12 to be entered.

Some other articles that might provide some insight -
http://help.dottoro.com/ljlkwans.php[^]
http://blog.pothoven.net/2008/05/keydown-vs-keypress-in-javascript.html[^]
 
Share this answer
 
v2

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900