1

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 well 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;
});

1 Answer 1

2

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/

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

2 Comments

Thanks . There is one problem I can not use Backspace to delete what I have typed. Can you fix that Please
@ pirzada if (e.which == 8) return true; added to my answer

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.