Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
71 events
when toggle format what by license comment
Apr 1, 2022 at 18:20 history edited Kar.ma CC BY-SA 4.0
Sorry, forgot to paste error message parameter. JS fiddle was already ok.
Apr 1, 2022 at 17:56 history edited Kar.ma CC BY-SA 4.0
Added CSS, I forgot about it.
Apr 1, 2022 at 17:50 history edited Kar.ma CC BY-SA 4.0
Added validity error message. Widely tested. Updated JS Fiddle accordingly
Nov 22, 2019 at 8:17 history edited emkey08 CC BY-SA 4.0
Improve code; refer to JSFiddle for additional input filters which the OP didn't ask for
Nov 11, 2019 at 10:10 history edited emkey08 CC BY-SA 4.0
Simplify answer (again)
Nov 11, 2019 at 9:44 history edited emkey08 CC BY-SA 4.0
Simplify answer
Sep 20, 2019 at 16:45 comment added Kamlesh This is not working in my case. Allow 1-99 value, minimum 1 and maximum 99, maxlength 2, negative value not allow, only numeric values (No symbol and alphabet). BTW Thanks for asking this question.
Aug 2, 2019 at 7:15 history edited emkey08 CC BY-SA 4.0
Cosmetics
Jul 18, 2019 at 6:24 history edited emkey08 CC BY-SA 4.0
Keep answer concise
Jul 12, 2019 at 12:51 history edited emkey08 CC BY-SA 4.0
Added more input filters, extended description
Jun 10, 2019 at 14:39 comment added CJPN Excellent post. For absolute clarity and to help those new to jQuery, perhaps make it clear that the "installation" phase should be performed within $(document).ready(function() { // Restrict input to digits by using a regular expression filter. $("#myTextBox").inputFilter(function(value) { return /^\d*$/.test(value); }); });
Jan 25, 2019 at 8:51 history edited emkey08 CC BY-SA 4.0
Add note about updated answer
Jan 10, 2019 at 9:19 history edited emkey08 CC BY-SA 4.0
Enhanced input filters and HTML 5 section
Dec 17, 2018 at 7:53 history edited emkey08 CC BY-SA 4.0
Added link to pure JavaScript version of this answer
Dec 8, 2018 at 17:45 history edited emkey08 CC BY-SA 4.0
Implement as plugin instead of function; fixed cosmetics
Dec 5, 2018 at 12:55 history edited emkey08 CC BY-SA 4.0
Even more simplified code
Dec 2, 2018 at 14:21 history edited emkey08 CC BY-SA 4.0
Bold major edit, but there is a *much* simpler solution for this - see JSFiddle
Jun 28, 2018 at 6:59 history edited Prince Patel CC BY-SA 4.0
Don't allow DOT .
Mar 15, 2018 at 17:17 history edited K.Dᴀᴠɪs CC BY-SA 3.0
added 31 characters in body
Nov 3, 2017 at 14:56 history edited Sylvan D Ash CC BY-SA 3.0
Updated minified snippet to allow Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X and prevent the letters: "acvx"
Sep 19, 2017 at 13:00 history edited Braian Mellor CC BY-SA 3.0
update of broken link
Jun 14, 2017 at 11:06 comment added Krunal Limbad however we can still copy text from any text source and paste it in textbox
Oct 27, 2016 at 11:59 comment added cloverink This for protected multiple dot if (e.keyCode == 190 && $(this).val().indexOf(".") > -1) { return false; }
Jul 13, 2016 at 16:31 history edited Alexey B. CC BY-SA 3.0
use === instead of == and no spaces inside this parentheses
Jun 12, 2016 at 16:28 comment added Valijon Don't take it. It doesn't prevent key combination, like alt + number, spanish accent key... Better answer: http://stackoverflow.com/a/7295864/3710490. Here is screenshot of error: http://img10.lostpic.net/2016/06/12/8dd58e87f2310edb79c25d8571fb6261.png
Jun 29, 2015 at 9:41 comment added Daniel Omine if ctrl+v a invalid char, the script do not detect.
Jun 24, 2015 at 12:00 comment added Vasil Popov Related to the upper issues and some additional needed features, how about this plugin: github.com/customd/jquery-number . Demo: texotela.co.uk/code/jquery/numeric and opensource.teamdf.com/number/examples/demo-as-you-type.html It has custom validation of a string value or can be attached to a component. It support live auto formatting during typing, customizable length of fraction digits and rounding, changing the coma symbol and the thousands separator symbol. It pretty sums everything from the discussed functionality here plus some useful extras
Jun 5, 2015 at 19:29 history edited SpYk3HH CC BY-SA 3.0
Add both a minimized version and a more versatile use of answer. Also added script for testing on answer. Question is already polluted with answer. I saw no point in just making a new one.
Apr 30, 2015 at 9:14 history edited William Isted CC BY-SA 3.0
e.ctrlkey does not support Mac Command+A, added e.metaKey to allow Select All from Mac
Mar 2, 2015 at 7:54 comment added user2718671 Works like a charm in firefox but in chrome you still can type this char ^ and also ´ this (at least on a qwertz-keyboard) - any idea how to prevent that?
Dec 5, 2014 at 3:28 history edited Muxa CC BY-SA 3.0
Allowed up button
Dec 5, 2014 at 3:27 comment added Muxa add event.keyCode == 40 for Up button
Sep 26, 2014 at 12:31 comment added Luc Wollants I agree with @MiniQuark about the azerty keyboards. On those keyboards you need to press shift. When using jQuery you can use the keypress eventhandler. For example: $('#nummer').keypress(function (event) { console.log('keypress: ' + event.which); if (!isNumber(event.which)) { event.preventDefault(); } }); function isNumber(characterCode) { var isNumber = false; if (characterCode >= 48 && characterCode <= 57) { isNumber = true; } return isNumber; }
Sep 7, 2014 at 13:32 comment added QMaster With thanks to all, I wrote another with min and max range. You could see in: stackoverflow.com/questions/2013229/… @miniQuark please tell me if you have any point.
Sep 4, 2014 at 11:12 review Suggested edits
Sep 4, 2014 at 11:29
Mar 14, 2014 at 10:15 comment added NoviceToDotNet How can i avoid 0 value i want 1 to 9, 10 is acceptable but single 0 should not be there.
Mar 10, 2014 at 18:17 history edited mawburn CC BY-SA 3.0
Added in keycode 110 for keypad decimal.
S Feb 11, 2014 at 21:21 history suggested CommunityBot CC BY-SA 3.0
improved code
Feb 11, 2014 at 21:17 review Suggested edits
S Feb 11, 2014 at 21:21
S Oct 8, 2013 at 20:44 history suggested nobane CC BY-SA 3.0
Added support for '.' and cleaned up initial keyCode check since user has access to jquery
Oct 8, 2013 at 20:37 review Suggested edits
S Oct 8, 2013 at 20:44
Sep 9, 2013 at 13:41 comment added Tim Truston Add this // Allow: . dot for decimals (event.keyCode == 110 || event.keyCode == 190)
Aug 9, 2013 at 1:54 review Suggested edits
Aug 9, 2013 at 2:02
Aug 4, 2013 at 16:04 comment added Richard Sitze How does the edit must made a moment ago address the problem? I'm not convinced it's a valid edit.
S Aug 4, 2013 at 15:54 history suggested Maxwell175 CC BY-SA 3.0
Add an answer for HTML5.
Aug 4, 2013 at 15:37 review Suggested edits
S Aug 4, 2013 at 15:54
Feb 5, 2013 at 22:23 comment added Ads I added event.keyCode != 116 && (event.keyCode != 86 && event.ctrolKey === true) && (event.keyCode != 67 && event.ctrolKey === true) to allow for F5 (and Ctrl-F5) for page refreshing, Ctrl-C, Ctrl-V because my users would have complained.. :-)
Dec 12, 2012 at 14:18 comment added MiniQuark I really disagree with this answer: it is really too risky playing with keycodes, because you cannot be sure what the keyboard layout is. For example, on French keyboards, users have to press shift in order to type numbers. So this code will not work at all. So please go for validation instead of this keycode hack.
Oct 10, 2012 at 5:16 comment added David Stinemetze @Nipzz The only issue I ran into with Macs was to make ⌘A work on Mac, I had to change (event.keyCode == 65 && event.ctrlKey === true) to (event.keyCode == 65 && (event.ctrlKey === true || event.metaKey === true ))
Sep 12, 2012 at 7:07 comment added LCJ @bažmegakapa Don't we need to allow Backspace and Delete keys?
May 18, 2012 at 17:05 review Suggested edits
May 18, 2012 at 21:46
May 9, 2012 at 9:56 comment added Episodex There is a problem with TexoTela numeric plugin. It disables "onchange" event for textbox. This issue was reported on gitHub a year ago and there is no response till now...
S May 1, 2012 at 1:37 history suggested CommunityBot CC BY-SA 3.0
also allow enter to allow form to be submitted
May 1, 2012 at 0:42 review Suggested edits
S May 1, 2012 at 1:37
S Apr 23, 2012 at 15:47 history suggested CommunityBot CC BY-SA 3.0
fix shift key [!@#$% etc]
Apr 23, 2012 at 14:39 review Suggested edits
S Apr 23, 2012 at 15:47
Mar 22, 2012 at 17:40 comment added Ruslans Uralovs You may also want to allow for function keys like F5 for example to refresh the page
Mar 22, 2012 at 16:32 comment added Jon Crowell Also add event.keyCode==110 to allow decimals from the numeric keypad.
Jan 19, 2012 at 18:34 comment added Anthony Queen Confirmed the shift+ bug. Also, ALT+ number pad allows pretty much anything (i.e. Alt+321 = A, Alt+322 = B, etc...). Another case for server side validation.
Jan 19, 2012 at 0:17 history made wiki Post Made Community Wiki by Pedro Soares
Jan 18, 2012 at 23:14 comment added Allen Rice ooo, just picked up an issue for this from our testers. You have to prevent the user from holding down shift and hitting the numeric keys, otherwise the input will allow !@#$%^&*()
Jan 17, 2012 at 18:22 history edited Allen Rice CC BY-SA 3.0
Adding escape and tab
Jan 17, 2012 at 18:15 history edited Allen Rice CC BY-SA 3.0
Adding the ability to allow for Ctrl+A, home, end, left, right
Jan 12, 2012 at 14:06 comment added Anthony Queen Add keyCodes 37 and 39 to allow left and right arrow navigation in the txt box for example: if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39)
S Sep 29, 2011 at 8:53 history suggested JT... CC BY-SA 3.0
Forgot to include event.keyCode from the number pad.
Sep 29, 2011 at 8:18 review Suggested edits
S Sep 29, 2011 at 8:53
Sep 16, 2011 at 6:32 comment added Michael L Watson Thanks! event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 190 if you want decimals
Sep 5, 2011 at 11:30 review Suggested edits
Sep 5, 2011 at 11:42
Jul 29, 2011 at 6:26 history edited kapa CC BY-SA 3.0
added 485 characters in body
Jun 15, 2009 at 12:33 vote accept djmzfKnm
Jun 15, 2009 at 9:26 history answered kgiannakakis CC BY-SA 2.5