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 (´¨ˆ˜).
-
1have you tried jquery validator !Venkat.R– Venkat.R2016-01-08 09:21:51 +00:00Commented 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'tNysa– Nysa2016-01-08 22:22:59 +00:00Commented Jan 8, 2016 at 22:22
-
check my fiddle which has fiddle url what you expectVenkat.R– Venkat.R2016-01-09 00:59:42 +00:00Commented 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 (´¨ˆ˜).Nysa– Nysa2016-01-09 02:52:55 +00:00Commented Jan 9, 2016 at 2:52
-
Please share your code snippetVenkat.R– Venkat.R2016-01-09 03:29:28 +00:00Commented Jan 9, 2016 at 3:29
|
Show 9 more comments
2 Answers
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/
Comments
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.
$('#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
Venkat.R
i'm able to enter character but it should not. i meant "e"
Mi-Creativity
Oh it is other browsers, Chrome doesn't let you
Venkat.R
i checked in chrome latest browser
Mi-Creativity
Couldn't reproduce in Chrome
Venkat.R
Nice its fixed with latest update. copy and paste the string. it should be restricted
|