Click here to Skip to main content
12,402,889 members (72,514 online)
Rate this:
 
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 23-Jan-13 5:40am
   
haii...now I have a problem if I enter only '.' then it will not checked ..it is prcossed
aravindnass 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 23-Jan-13 5:40am
   
haii...now I have a problem if I enter only '.' then it will not checked ..it is prcossed
aravindnass 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2016
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