javascript - jQuery method that disallows input of certain chars -
i'm trying make jquery method delete wanted chars selected elements.
for example:
$("input").disallowchars(/\d/g);// should disallow input of non-digit elements in input elements this how thought it, doesn't seem work:
$.fn.disallowchars = function(regexp){ this.keyup(function(){ var value = $(this).val(); value.replace(regexp, ""); $(this).val(value); }); return this; }; $("input").disallowchars(/\d/g); i'm total newbie @ this, how can make work.
thanks
you use string.fromcharcode() , keypress event instead:
$.fn.disallowchars = function(regexp){ return this.keypress(function(e){ if(string.fromcharcode(e.which).match(regexp)) return false; }); }; but doesn't disable characters paste in input using mouse or paste keyboard shortcut.
on modern browsers, use input event, or change keyup paste mouseup (ya mouseup, handle dropped text too):
$.fn.disallowchars = function(regexp){ return this.on('input', function(){ this.value = this.value.replace(regexp, ''); }); }; but once input value replaced, text carret put in end (or start depending browser behaviour) of string input.
Comments
Post a Comment