Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<script type="text/javascript">
    $(function() {
    $('.textBox_for6tds').keydown(function(e) {
            if (e.shiftKey || e.ctrlKey || e.altKey) {
                e.preventDefault();
            } else {
                var key = e.keyCode;
                if (!((key == 8)  || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)))
                {
                    e.preventDefault();
                    alert("Please enter numeric value!");

                }
            }
        });
    });



--everything is working fine.
--but if i want to enter 43.53
--the . if i enter its not allowing me to do.......
--what is the ascii of .(decimal). Or how to handle this.
Posted
Comments
Thanks7872 1-Oct-13 5:02am    
May i know why you are doing all this? I mean what this code is expected to do?
anurag19289 2-Oct-13 9:38am    
Hi rohan-

in the textbox user can enter numbers as well as decimal. but the ascii of decimal was not working. But i got to know that the ascii of '.' is 190 but not 46

Hi,

The dot in numeric keypad section ASCII value is 46. But the dot in general the key ascii value is 190.

if (!((key == 8) || (key == 46) || (key == 190) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)))
{
e.preventDefault();
alert("Please enter numeric value!");

}
 
Share this answer
 
Comments
Brian A Stephens 1-Oct-13 10:00am    
Now that makes sense!
anurag19289 1-Oct-13 10:12am    
This is superb..... +5
NOw this is what i was looking for. Its working perfectly fine now.
anurag19289 6-Nov-13 8:15am    
But when i am trying the dot in numlock section[numbers in right side of the keyboard]. Getting the error. But it works fine for the key near comma(,).
You are testing for a strange collection of ASCII values. Your allowed values are:
 8      [backspace]
46      .
35      #
36      $
37      %
38      &
39      '
40      (
48-57   digits 0-9
96      `
97-105  letters a-i

You may want to visit http://www.asciitable.com/[^] to see what you really want.

I'm not sure why you can't type a period, but that seems to be only one of many problems with the code.
 
Share this answer
 
v2
There are JQuery plugins which make this sort of stuff much easier. If you use the filter_input plugin, then you can simply use:
JavaScript
$('.textBox_for6tds').filter_input({ regex: '[0-9.]' });
 
Share this answer
 
v2
see ASCII of decimal "." is 46. and you are checking for 46 that is the reason you are not allowed to enter the decimal.

If you want to allow decimal just remove the condition for 46.
Hope that help you.

-SG
 
Share this answer
 
Comments
anurag19289 1-Oct-13 4:53am    
Hi Gyan-

I have mentioned as not equal to.(!)
if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)))

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