Remove HTML TAG Inputs from input Form

2019-02-26 03:32发布

问题:

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/

回答1:

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


回答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.



回答3:

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/



回答4:

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


回答5:

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