Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


XML
<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>






C#
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

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




JavaScript
$('#txtCloseReading').keypress(function (e) {
        if ((e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) && e.which != 46) {
            return false;
        }
});
 
Share this answer
 
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
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'))
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900