/ Published in: JavaScript
This tiny jQuery plug in forces a user to enter only numeric values on an input field by silently removing non-numeric values as they're entered. (Caution: Never rely on client-side validation; use server-side validation as well)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
(function ($) { $.fn.forceNumeric = function () { return this.each(function () { $(this).keyup(function() { if (!/^[0-9]+$/.test($(this).val())) { $(this).val($(this).val().replace(/[^0-9]/g, '')); } }); }); }; })(jQuery); // Usage example: $('input.numeric').forceNumeric();
URL: http://terraduo.com