最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to allow only defined characters as input using jQuery? - Stack Overflow

programmeradmin1浏览0评论

How do i allow special characters such as hyphen,ma,slash,space key,backspace key,delete key along with alphanumeric values and restrict the rest in jQuery?

As this criteria(allowed characters/input values) varies from field to field, i would like to make it as a utility method which accepts input field id and allowed characters as parameters. for example: limitCharacters(textid, pattern)

How do i allow special characters such as hyphen,ma,slash,space key,backspace key,delete key along with alphanumeric values and restrict the rest in jQuery?

As this criteria(allowed characters/input values) varies from field to field, i would like to make it as a utility method which accepts input field id and allowed characters as parameters. for example: limitCharacters(textid, pattern)

Share Improve this question edited Aug 16, 2012 at 11:30 Adi 5,1796 gold badges35 silver badges48 bronze badges asked Aug 16, 2012 at 11:23 net usernet user 5932 gold badges8 silver badges21 bronze badges 2
  • 1 Do you have any code samples of your attempts? – o01 Commented Aug 16, 2012 at 11:27
  • Thanks you all for ur help... Now i need to pare non-english characters.. such as Azhari.. How do i do it with pattern match? Basically my application should support for both English and Azhari characters.. Here i am struck. Please Help me out... – net user Commented Aug 16, 2012 at 17:32
Add a ment  | 

3 Answers 3

Reset to default 6

​You can just check the keyCode on keydown and run preventDefault() if match:

$('input').keydown(function(e) {
    if (e.which == 8) { // 8 is backspace
        e.preventDefault();
    }
});​

http://jsfiddle/GVb6L/

If you need to restrict to certain chars AND keyCodes + make it into a jQuery plugin, try something like:

$.fn.restrict = function( chars ) {
    return this.keydown(function(e) {
        var found = false, i = -1;
        while(chars[++i] && !found) {
            found = chars[i] == String.fromCharCode(e.which).toLowerCase() || 
                    chars[i] == e.which;
        }
        found || e.preventDefault();
    });
};

$('input').restrict(['a',8,'b']);​

http://jsfiddle/DHCUg/

I did something like this but in jQuery plugin format. This example will only allow numbers and full stops.

You can call this by writing:

$("input").forceNumeric();

And the plugin:

            jQuery.fn.forceNumeric = function () {

             return this.each(function () {
                 $(this).keydown(function (e) {
                     var key = e.which || e.keyCode;

                     if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                     // numbers   
                        key >= 48 && key <= 57 ||
                     // Numeric keypad
                        key >= 96 && key <= 105 ||
                     // ma, period and minus, . on keypad
                        key == 190 || key == 188 || key == 109 || key == 110 ||
                     // Backspace and Tab and Enter
                        key == 8 || key == 9 || key == 13 ||
                     // Home and End
                        key == 35 || key == 36 ||
                     // left and right arrows
                        key == 37 || key == 39 ||
                     // Del and Ins
                        key == 46 || key == 45)
                        return true;

                    return false;
                });
            });
        }

I would suggest using David solution for modifier keys like backspace and delete and this code below for characters:

var chars = /[,\/\w]/i; // all valid characters
$('input').keyup(function(e) {
  var value = this.value;
  var char = value[value.length-1];
  if (!chars.test(char)) {
    $(this).val(value.substring(0, value.length-1));
  }
});

Also, I've experienced some problems with keydown so I'd do it on keyup.

Demo: http://jsfiddle/elclanrs/QjVGV/ (try typing a dot . or semicolon ;)

发布评论

评论列表(0)

  1. 暂无评论