1

I tried to find one but I couldn't find any. I want a jQuery code that will accepts only numbers, of course allow "Ctrl+aC, "Ctrl+c", "Ctrl+v", "Ctrl+x", "command+a", "command+c", "command+v", "command+x", "home", "end", "left", "right", "up" and "down" and doesn't allow extended ASCII specially (´¨ˆ˜) because no matter what was the code when I tested it with the "alt + n" , "e" , "u" or "i" it will show these (´¨ˆ˜).

14
  • 1
    have you tried jquery validator ! Commented Jan 8, 2016 at 9:21
  • this code really prevents extended ascii, but is it possible to use it without submitting I think no, right ? also I want to prevent the user from entering anything else except numbers before submission. whenever keydown check then allow it or not if it's number print it in the textbox else don't Commented Jan 8, 2016 at 22:22
  • check my fiddle which has fiddle url what you expect Commented Jan 9, 2016 at 0:59
  • I tried this one the problem is that it allows (´¨ˆ˜) these are alt+some of the keys also It doesn't allow command+a, command+c, command+v and command+x but I have an idea how to fix this problem. the only thing that I couldn't find a solution for it —> these (´¨ˆ˜). Commented Jan 9, 2016 at 2:52
  • Please share your code snippet Commented Jan 9, 2016 at 3:29

2 Answers 2

3

Use jQuery Validator

Check the library for you identify any keys which is written by me. https://github.com/ramsunvtech/ejs/blob/master/e.js

How to E.js?

E.key.isHome();
E.key.isAlphabet();

Refer this on How to detect Ctrl+V, Ctrl+C using JavaScript?

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Makes "field" required and a decimal number only.</title>
    <link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css">
</head>
<body>
    <form id="myform">
        <label for="field">Required, decimal number: </label>
        <input class="left" id="field" name="field">
        <br/>
        <input type="submit" value="Validate!">
    </form>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
    <script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
    <script>
        // just for the demos, avoids form submit
        jQuery.validator.setDefaults({
            debug: true,
            success: "valid"
        });
        $("#myform").validate({
            rules: {
                field: {
                    required: true,
                    number: true
                }
            }
        });
    </script>
</body>
</html>

Fiddle URL for key validation:
http://jsfiddle.net/4m5cfe16/

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

Comments

0

UPDATED by checking the input value on each keypress or input event, and if the inserted char is not a number, even the ´¨ˆ˜ , we filter it out , but still support keyboard copying and pasting, home end, up, down, left, and right.

JS Fiddle

$('#theField').on('keypress input', function() {
  var value = $(this).val();
  value = value.replace(/\D+/, ''); // removes any non-number char
  $(this).val(value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form action="" id="theform">
  <input type="text" id="theField" name="theField">
  <button id="theBtn">Submit</button>
</form>

9 Comments

i'm able to enter character but it should not. i meant "e"
Oh it is other browsers, Chrome doesn't let you
i checked in chrome latest browser
Couldn't reproduce in Chrome
Nice its fixed with latest update. copy and paste the string. it should be restricted
|

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.