使用JQuery,如何防止用户在输入字段中输入脚本或html? [英] Using JQuery, how can I prevent users from entering script or html into input fields?

查看:160
本文介绍了使用JQuery,如何防止用户在输入字段中输入脚本或html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我需要能够在处理之前从输入字段中删除脚本和html.我正在使用JQuery,并希望找到一种执行此类操作的标准方法.有什么想法吗?

Basically, I need to be able to strip script and html from input fields before processing. I am using JQuery, and was hoping to find that there is a standard way of doing that sort of thing. Any ideas?

推荐答案

您的html:

<input class="striphtml" name="name" />

您的js:

$(document).ready(function() {
  // strip all html when text in input changes
  $(".striphtml").change(function(){
      $(this).val( $(this).text() );
  });
});

结果:

<p>This is a test.</p>

将替换为:

&lt;p&gt;This is a test.&lt;/p&gt;

注意:如SLaks所述,您必须在服务器端验证您的输入!!

Attention: As SLaks mentioned you MUST validate your INPUTS on server side!!!

这篇关于使用JQuery,如何防止用户在输入字段中输入脚本或html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆