Click here to Skip to main content
11,715,022 members (73,642 online)
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 20: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 Sergey Alexandrovich Kryukov 520
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | 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