Click here to Skip to main content
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 19: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 545
1 OriginalGriff 285
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
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