Remove HTML TAG Inputs from input Form

2019-02-26 04:00发布

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/

5条回答
Explosion°爆炸
2楼-- · 2019-02-26 04:02
var chars = new Array("<", ">" ,"/" ,"\\");
$("#in1").keypress(function (evt) {
    if(!(chars.indexOf(String.fromCharCode(evt.which)) > -1))
        return false;
});
查看更多
疯言疯语
3楼-- · 2019-02-26 04:03

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-26 04:06

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;
});
查看更多
smile是对你的礼貌
5楼-- · 2019-02-26 04:06

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;
});
查看更多
戒情不戒烟
6楼-- · 2019-02-26 04:16

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/

查看更多
登录 后发表回答