0

I have the below script:

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

 $(function () {
    $("#<%= txtAmount.ClientID %>").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();
            }
        }
    });

What i am trying to do is allow the decimal character (.) but prevent all other characters except for numbers so the only allowed characters are numbers and a decimal.

I read up on keycodes for JQuery which states 110, 190 is the decimal character but the above code allows those values, i read up on javascript which has 46 as the decimal keycode but removing that from the code above didnt work either. Could anyone advise?

2
  • Where exactly does it allow 190 ? Commented May 17, 2014 at 12:20
  • My understanding of this line "if .... (key >= 96 && key <= 105)))" is that it allows characters above 190?? Suppose im wrong since your asking? Commented May 17, 2014 at 12:36

2 Answers 2

1

Seems like there should be an easier way to do that

$("#<%= txtAmount.ClientID %>").keypress(function (e) {
    if (!String.fromCharCode(e.which).match(/[\d.]/)) e.preventDefault();
});

FIDDLE

Sign up to request clarification or add additional context in comments.

4 Comments

This didnt work. Ive have added the script declarations in the original post, in case i am targeting the incorrect version.
Works in Fiddle but not on my webpage? I tried changing it to version 1.11.0 but that didnt work either.
@Computer - If it works in the fiddle, but not on your webpage, something is clearly wrong on your end, as it's just a simple regex, not much that could go wrong ?
This is strange, the keyboard layout restricts the decimal point from the main layout but the numerical layout on the right allows the decimal??
0

here is a chunk:

$(document).ready(function () {
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && e.which.indexOf('.') != -1 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });


});

Fiddle DEMO

For your code:

$(function () {
    $("#<%= txtAmount.ClientID %>").keydown(function (e) {
         //if the letter is not digit then display error and don't type anything
         if (e.which != 8 && e.which != 0  && e.which.indexOf('.') != -1 && (e.which < 48 || e.which > 57)) {
            //display error message
            $("#errmsg").html("Digits Only").show().fadeOut("slow");
                   return false;
        }
    });

   });

6 Comments

Top code allowed all characters where the bottom code almost works but didnt allow the decimal. I have posted the declarations in my original post in case that has anything to do with it.
my code only allows numbers is'nt that you asked in question?
No, my requirement was "only allowed characters are numbers and a decimal"
means you want . to be allowed?
Yes so this is what im after 12.34 but i dont want any characters allowed (i.e. A-Z, a-z) or any special characters (!"£$%$% etc)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.