Introduction
This article is about how we can prevent users from entering some characters like comma, a,b...etc.
Background
We can use two methods either a client side method or a server side method. A client side method would be preferable because its is so fast, while the server side method needs postback.
Using the code
We can use the jquery and javascript. For which first u add the reference of the jquery library.
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
Then we need to write code on .keypress function. We use keyypress function instead of keydown and keyup because if we use we can prevent from entering into the textbox, ie the key press is being prevented when the user press the key. But key down and key up functions allow the users to enter the value into the text box and then validate the entered value.
In keypress event we will not get the keyCode instead we can use charcode. keyCode says something
about the actual keyboard key the user pressed, while charCode gives the ASCII value of the resulting character. These
bits of information need not be the same; for instance, a lower case 'a' and an upper case 'A' have the same keyCode,
because the user presses the same key, but a different charCode because the resulting character is different.
if (e.charCode == 44 || e.which == 44) {
if ($.browser.msie) // for IE7
event.returnValue = false;
return false;
} Here we evaluate which key is being pressed and take action accordingly.
**charCode Property indicates the Unicode for the key pressed. Use
String.fromCharCode(charCode) to convert code to string.
NS/Firefox only.
List of charcodes
32 [Spac]
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
For getting the full code and check out my blog post http://onlinedotnettutorials.blogspot.in/2012/09/disable-or-remove-comma-in-asp-textbox.html
History
Keep a running update of any changes or improvements you've
made here.