【问题标题】:Remove HTML tags from input before sending在发送之前从输入中删除 HTML 标签
【发布时间】:2020-02-25 06:07:18
【问题描述】:

我有一些聊天脚本,我正在尝试在单击发送或提交之前过滤输入字段。 这是我的输入文件:


<input required type="text" name="content"  maxlength="300" id="content" placeholder="Write Your Message.."  autocomplete="off">

提交

 <button type="submit" class="sound-btn" id="submit_button">
        <i class="fa fa-paper-plane"></i>
    </button>

当我尝试发送一些标签时

<img src="avatar/user1_13128662_tumb.jpg">
<div id="test">
demo text
</div>

不幸的是工作

我需要做什么.. 从输入中删除所有attr标签或阻止在输入中写入html标签,因为所有这些数据都保存在数据库中并且非常危险并且也允许注入xss 像这样的

<b>
<div>
</div>
<img
<a
<li
<ul
and all the HTML tags just keep links and numbers and normal text

我认为所有的 HTML 标签 我需要保留的 http 和 https 和 mailto: 以及普通的文本和数字 只需过滤不需要的字符,然后通过 javascript 或 jquery 发送 ..thanks

【问题讨论】:

    标签: javascript php jquery html regex


    【解决方案1】:

    您可以对输入的 onchange 事件应用一个简单的正则表达式:

    document.getElementById('content').addEventListener('change', (e)=> {
        let tValue = e.target.value.replace(/<[^>]+>/gim, '');
        e.target.value = tValue;
    });
    

    【讨论】:

      【解决方案2】:

      在发送到数据库之前使用正则表达式,用“

      【讨论】:

      • 使用类似的东西。 s/]*>//g
      猜你喜欢
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 2017-05-19
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多