2

When user type text a, b, c is ok.

If user type another text like 0, 9, z, x.... alert('can't type this word');

How to make it easily ?

3
  • The best way to achieve this is using regular expressions. Commented Nov 26, 2011 at 11:46
  • let me make it clear ... you have an input type text and you want to use jquery to test whether the entered value is ''a'', ''b'', or ''c'' and in other cases alert something? Commented Nov 26, 2011 at 11:48
  • You right Elwhis,would you mind show me the example? Commented Nov 26, 2011 at 11:52

3 Answers 3

1

Do something like this:

$(document).ready(function() {
  $("#yourInputId").keyup(function() {
     if($(this).val() == "z") {
        alert("You cant type this");
     }
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

^^ good, but if user type over one word,like 'azbc',how to check it??
@user962408, if( $(this).val().match(/[^abc]/) ) {alert("unallowed characters");}
1

This is the link to allow Number and not alphabets (check the code here and make changes)....
Help your self finding out the key code for whichever alphabet you need to allow...
Here is the keycode

Comments

1

You can try use regular expression. example: http://www.9lessons.info/2010/01/jquery-validation-with-regular.html

or setup a powerful plugin like: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Comments

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.