JQuery Keyboard events & Regex logic
I have TextBox in which user can type only Alphabets(Lower&Upper), Hyphen (-), BackSpace only. These will be basically any Qualified URL name page like Contact/my-contact etc
I need Regex and restriction of keyboard keys on that Textbox.
For keyboard logic below code is not working properly as w开发者_StackOverflow社区ell so need help
jQuery('#UrlName').keypress(function (e) {
code = e.keyCode ? e.keyCode : e.which;
if ((code.toString() > 45 || code.toString() < 57) && (code.toString() < 97 || code.toString() > 122)) return false;
});
Like this:
jQuery('#UrlName').keypress(function (e) {
if (e.which == 8) return true;
if (!/[A-Z\-]/i.test(String.fromCharCode(e.which))) return false;
});
example: http://jsfiddle.net/niklasvh/77feF/
精彩评论