Jquery Plugin to only allow numeric input..

Ok.. so this is my first Jquery Plugin – VERY SIMPLE…

All it does is attach to the keydown of any element it is attached to and look to see if the key being pressed is numeric via a regular expression. Nothing fancy. no periods, no commas, no currency formatting.

Useful for making sure credit card numbers or bank account numbers are being entered correctly in a web page.

(function($) {
    $.fn.PreventNonNumeric = function() {
        $(this).keypress(
            function(x) {
                if (x.key.replace(/\D/, "") == ""|| x.key === "Spacebar")
                { x.preventDefault(); }
            }
        );

        return this;
    };
})(jQuery, event);

Attach to an element as

$("#Text1").PreventNonNumeric();

 

 

Kill “right click” from a webpage with JQuery

This was always a pain with pure javascript, however Jquery has made our lives infinatly easier. Just bind to the “contextmenu” object and kill any action.

$(function(){
$(document).bind(“contextmenu”,function(e){
return false;
});
});