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 (´¨ˆ˜).

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Nysa
  • 425
  • 1
  • 5
  • 14
  • 1
    have you tried jquery validator ! – Venkat.R Jan 08 '16 at 09: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 – Nysa Jan 08 '16 at 22:22
  • check my fiddle which has fiddle url what you expect – Venkat.R Jan 09 '16 at 00: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 (´¨ˆ˜). – Nysa Jan 09 '16 at 02:52
  • Please share your code snippet – Venkat.R Jan 09 '16 at 03:29
  • I tried these: http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery , http://jsfiddle.net/yrshaikh/44pc78pj/ , https://snipt.net/GerryEng/jquery-making-textfield-only-accept-numeric-values/ , http://www.tricksofit.com/2014/10/allow-only-numbers-in-textbox-jquery#.VpCK1zZtlsM , and a lot approximately similar but nothing work with these (´¨ˆ˜) they always appear. – Nysa Jan 09 '16 at 04:29
  • can u please share your code – Venkat.R Jan 09 '16 at 04:51
  • the one that I'm working on ? I don't have one yet because nothing work so, I deleted every single code that didn't work and the links that I shared with you are some of them no matter what was the code whenever I press the alt key + n ,e,u or i these will appear(˜´¨ˆ) – Nysa Jan 09 '16 at 05:08
  • so add that in your question – Venkat.R Jan 09 '16 at 05:10
  • sorry if my question was unclear and thank you for your help. – Nysa Jan 09 '16 at 05:20
  • you are welcome @Nysa, Accept the answer, if my answer solved your problem else edit your question and let me know your current issue. – Venkat.R Jan 09 '16 at 07:33
  • thank you @Venkatraman . here my edited question I wish it will be solved. – Nysa Jan 09 '16 at 09:15
  • check the title i'm sure its works out for you ! – Venkat.R Jan 09 '16 at 09:27
  • @Nysa, check whether my answer is what you're looking for – Mi-Creativity Jan 09 '16 at 10:17

2 Answers2

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/

Community
  • 1
  • 1
Venkat.R
  • 7,420
  • 5
  • 42
  • 63
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>
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47