jQuery Plugin – Numeric

Allows only valid characters (i.e. numbers) to be typed into a text box. Can take negative numbers and a decimal point. You can supply a callback that runs when focus is lost and the value in the text box is not a valid number.

Source code available from GitHub or a Zipped download.


$("input.numeric").numeric()


$("input.numeric").numeric(null, numberInvalid). Calls numberInvalid if the text in the box is not a valid number. As null is the first parameter, the default decimal character (“.”) is used


“,” decimal separator $("input.numeric3").numeric(",")


Even works in textarea’s. $("textarea.numeric").numeric();

Notes

Disabling pasting does not work under all browsers (fine in IE), so this feature has been removed. The old version is still available though (if anyone can figure out how to prevent paste in all browsers).

Limitations / Bugs

  • Does not validate (best left for another script)
  • Cannot make the number negative if there is already text inside the input box

3 thoughts on “jQuery Plugin – Numeric

  1. […] time since I’ve posted, but have updated my jQuery numeric plugin for allowing only numbers in input boxes, fixing a couple of bugs and adding functionality […]

  2. […] a look at this plug-in (TexoTela jQuery numeric). This (jStepper) is another […]

  3. […] From back to front For the backend we use our internally-developed PHP MVC-framework. Whilst writing the code for the framework we spent a lot of time digging through and analyzing best-practices and solutions found in major PHP frameworks, such as Zend and CakePHP. The result is a tailor-made PHP framework which suits TimeApp perfect. It’s fast, agile and memory efficient. The frontend of TimeApp is generated by HTML accompanied by CSS3 and JavaScript for added functionalities and visual presentation. We use Bootstrap (currently version 2.3.2) and jQuery extensively throughout the application. The jQuery package is also extended by a couple of rather awesome third-party plug-ins (Chart.js, Select2, qTip, Accounting and Numeric). […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>