Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NETC#4.0
In my web application i have morethan 50 textboxes in a single page. Only numeric values are allowed to type inside these texboxes. If a letter is typed then it must show a error message. Right now i did this by using javascript function and calling it in every text box "onkeypress" . Is there any other method to do this. I Dont want to add onkeypress attribute in every textbox. I places all my textboxes in a panel.
<script type="text/javascript">
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            if ((charCode >= 48 && charCode <= 57) || charCode == 08 || charCode == 43 || charCode == 45 || charCode == 127) {
                return true;
            else {
                alert('Please Enter Numbers Only');
                //return false;
                 evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
<asp:TextBox ID="txtnumber" runat="server" MaxLength="6"  onkeypress="return isNumberKey(event)" >
Posted 19-Feb-13 22:51pm
Edited 20-Feb-13 20:58pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can take help from jquery object and bind event handler like as follows
DOM ready event handler
     isNumberKey(event);//call your javascript function
My suggestion is you assign a commin css class to each textbox you want to validate and use this css class from jquery method like as follows
     isNumberKey(event);//call your javascript function
shamjid at 20-Feb-13 7:30am
@Ahasan: First of all thanks for ur response. I tried ur code,its working but a small problem. If i tried to type a letter it shows the alert message but it also insert the letter in the texbox. If i type "asd" whenever i typed "a" it shows the alert message ,but it insert "a" in the textbox..
S. M. Ahasan Habib at 20-Feb-13 11:11am
instead of return false you should use event.preventDefault ? event.preventDefault() : event.returnValue = false; I already update your method IsNumericKey now it will work. Please Re run your code.
shamjid at 21-Feb-13 0:57am
@Ahasan:The problem is with firefox only..It works fine in chrome and IE
S. M. Ahasan Habib at 21-Feb-13 1:32am
So if you want to solve it with all browsers then you use this statement instead of just return false. event.preventDefault ? event.preventDefault() : event.returnValue = false;
shamjid at 21-Feb-13 1:46am
@Ahasan: I aready used ur statement..but still not working with Firefox
S. M. Ahasan Habib at 21-Feb-13 2:59am
yes one typeo is there evt will be there instead of event. I update your question. Please read it again and execute it firebox now and see the result. I tested it in firefox and found that it is working.
shamjid at 21-Feb-13 3:49am
@Ahasan:-Its working fine..Thanks a lot..5
shamjid at 21-Feb-13 6:28am
@Ahasan:-please go through the below link.This is my another ? I hope u ll get me some solution..Thank u
S. M. Ahasan Habib at 21-Feb-13 9:27am
I provided solution for that problem too. Please visit that link which you mention above and make sure it will work for you.
shamjid at 25-Feb-13 6:38am
@Ahasan:-Thanks bhai...u solved my two question..ur codes works fine..i need other help from you..please check below link
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Have a look here[^] It might be the cleanest way to do things.

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

  Print Answers RSS
Your Filters
0 Guruprasad.K.Basavaraju 490
1 Shai Vashdi 339
2 Sergey Alexandrovich Kryukov 291
3 Peter Leow 135
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,335
2 Peter Leow 4,040
3 Maciej Los 3,540
4 Abhinav S 3,308

Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid