1

How do i allow special characters such as hyphen,comma,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)

2
  • 1
    Do you have any code samples of your attempts? Commented Aug 16, 2012 at 11:27
  • Thanks you all for ur help... Now i need to compare 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... Commented Aug 16, 2012 at 17:32

3 Answers 3

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.net/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.net/DHCUg/

Sign up to request clarification or add additional context in comments.

1 Comment

This doesnt work with some chars such as 0 and / for instance, but is a nice function to restrict letters and numbers.
0

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 ||
                     // comma, 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;
                });
            });
        }

Comments

-1

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.net/elclanrs/QjVGV/ (try typing a dot . or semicolon ;)

1 Comment

Fails if you keep the . key pressed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.