Click here to Skip to main content
11,478,896 members (57,962 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET C#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 23:51pm
Edited 20-Feb-13 21: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
0 Sergey Alexandrovich Kryukov 200
1 F-ES Sitecore 195
2 Frankie-C 120
3 OriginalGriff 105
4 DamithSL 95
0 Sergey Alexandrovich Kryukov 7,880
1 OriginalGriff 7,341
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 21 Feb 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