Click here to Skip to main content
11,576,354 members (62,667 online)
Rate this: bad
good
Please Sign up or sign in to vote.
heyyy...

I want a javascript that allows only numerics and dot(.) also allow backspace ..

the purpose is to enter amount(price) in a txtbox...


I used a javascript but it does not allow backspace ...check it... and give a solution


<asp:TextBox ID="txtCloseReading" runat="server" attr-identity="CloseReading" MaxLength="10" CausesValidation="true"
                                            oncopy="return false" onpaste="return false" onkeypress="return NumberKeyAndDotKey(this.value,event);"></asp:TextBox>





function NumberKeyAndDotKey(StringValue, evt)
           {
               var status = false;
               var charCode = (evt.which) ? evt.which : event.keyCode;
 
               if (charCode == '46' ||  (charCode >= '48' && charCode <= '57')) {
 
                   status = true;
                   if (contains(StringValue, '.')) {
 
                   if(  (charCode >= '48' && charCode <= '57'))
                   {
                   status = true;
                   }
                   else
                   {
                   status = false;
                   }
 

 

                   }
               }
               return status;
 
           }
Posted 21-Jan-13 18:03pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<asp:TextBox ID="txtCloseReading" runat="server" attr-identity="CloseReading" MaxLength="10" CausesValidation="true"oncopy="return false" onpaste="return false"></asp:TextBox>



$('#txtCloseReading').keypress(function (e) {
        if ((e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) && e.which != 46) {
            return false;
        }
});
  Permalink  
v2
Comments
aravindnass at 23-Jan-13 5:40am
   
haii...now I have a problem if I enter only '.' then it will not checked ..it is prcossed
aravindnass at 23-Jan-13 5:41am
   
plz help ..it allow or accepts '.' dot without entering the any numerics
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

allow the ascii for backspace also like you have written for dot(.) and all numbers.
write your if condition as give bellow

if (charCode == '8' || charCode == '46' ||  (charCode >= '48' && charCode <= '57'))
  Permalink  
Comments
aravindnass at 23-Jan-13 5:40am
   
haii...now I have a problem if I enter only '.' then it will not checked ..it is prcossed
aravindnass at 23-Jan-13 5:41am
   
plz help ..it allow or accepts '.' dot without entering the any numerics

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 251
1 Abhinav S 135
2 Lucifier Rocks 105
3 Dave Kreskowiak 80
4 F-ES Sitecore 80
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 626
2 Abhinav S 483
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 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