Click here to Skip to main content
Rate this: bad
good
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 asp.net c#.
 
Hlep me ! thank you in advance
Posted 18-Jan-13 21:34pm
Comments
deepakaitr12345 at 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
Rate this: bad
good
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 -
http://help.dottoro.com/ljlkwans.php[^]
http://blog.pothoven.net/2008/05/keydown-vs-keypress-in-javascript.html[^]
  Permalink  
v2
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2014
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