简体   繁体   English

从输入表单中删除HTML TAG输入

[英]Remove HTML TAG Inputs from input Form

Ok basically when I type , it won't allow it. 好的,基本上,当我键入时,它将不允许它。 I want to add more like < > / \\ etc how do I do it? 我想添加更多类似<> / \\ etc的方法吗?

$("#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/ http://jsfiddle.net/QshDd/38/

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

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. 另一种解决方案,可以使用regEx或XMLParser或JSON解析器方法。

if you want something like this 如果你想要这样的东西
<input type="text"> ===> input typetext <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/ http://jsfiddle.net/QshDd/61/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM