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