2

Ok basically when I type , it won't allow it. I want to add more like < > / \ etc how do I do it?

$("#in1").keypress(function (evt) {
    if (String.fromCharCode(evt.which) == ",")
        return false;
});


<input type="text" id="in1">

Can see the demo here. http://jsfiddle.net/QshDd/38/

2
  • what exactly u want to ask? please clear your question Commented Feb 25, 2014 at 11:11
  • Basically it's a signup form, it need to remove html tags etc on submit but this is easier then done. Commented Feb 25, 2014 at 11:14

5 Answers 5

4

If you have a list of disallowed characters, you can forbid them in this fashion:

$("#in1").keypress(function (evt) {
    return [',', '<', '>', ].indexOf(String.fromCharCode(evt.which)) === -1;
});
Sign up to request clarification or add additional context in comments.

Comments

2

its working , you Required to give more conditions:

$("#in1").keypress(function (evt) {
if (String.fromCharCode(evt.which) == ",")
    return false;
if (String.fromCharCode(evt.which) == "<")
    return false;
if (String.fromCharCode(evt.which) == ">")
    return false;
if (String.fromCharCode(evt.which) == "\\")
    return false;
});

Another Solution , either use regEx or use XMLParser or JSON parser methods.

2 Comments

It does work, just too long, I never voted though. I voted now anyways, thanks!
no worry friend, Use REgEx, best way to do it, short and simple
1

if you want something like this
<input type="text"> ===> input typetext

  $("#in1").keypress(function (evt) {
    if (isValid(String.fromCharCode(evt.which)))
        return false;
    });

    function isValid(str){
      return /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
    }

http://jsfiddle.net/QshDd/61/

Comments

0

Try this, if you need only alphabets

$("#in1").keypress(function (evt) {
    var expr = /^([a-zA-Z\s]+)$/i;
    if (!String.fromCharCode(evt.which).match(expr))
        return false;
});

Comments

0
var chars = new Array("<", ">" ,"/" ,"\\");
$("#in1").keypress(function (evt) {
    if(!(chars.indexOf(String.fromCharCode(evt.which)) > -1))
        return false;
});

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.