Click here to Skip to main content
Click here to Skip to main content

Asp.net Textbox Remove Comma

By , 6 Sep 2012
 

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.

License

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

About the Author

Christopher John Paul
Software Developer
India India
Member
I am a software developer with a passion to develop innovative ideas.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanojsawkar6 Sep '12 - 23:55 
KeyPress
KeyDown
KeyUp
CharCode
KeyCode

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by Christopher John Paul
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid