aspx页面中<input>中特殊字符导致提交不安全信息问题的解决方案

本文介绍了一种在ASPX页面中处理用户输入特殊字符的方法,通过JavaScript实现了对&lt; 和 &gt;等特殊字符的过滤及转义,确保了后台接收数据的安全性。

在页面中的input标签中提交特殊字符,如&," 符号,后台文件在接受数据时会认为是不安全的输入内容,引起黄页异常,解决方案如下:

aspx文件代码:

<input type="text" id="txtFooterName" name="txtFooterName" class="tx_input_1"/>

<a href="javascript:if(checkInfo())submitform();"><div class="button_2" >确定</div></a>

JS文件代码:

 $(function(){
    String.prototype.enHtml = function () {
    return this.replace(new RegExp("<", "gm"), "&lt;").replace(new RegExp(">", "gm"), "&gt;");
    }
 )}
 
 
 function submitform() {
     //判断不能有特殊字符
if (CheckStr($.trim($("#txtFooterName").val()))!="") { alert("标题不能包含“" + CheckStr($.trim($("#txtFooterName").val())) + "”号"); $("#txtFooterName").focus(); return; }
     //替换<,>
     
else { var footname = $("#txtFooterName").val().enHtml(); $("#txtFooterName").val(footname);

document.forms["form1"].submit(); } } //检测特性字符,包含返回特殊字符 function CheckStr(str) { var SpecialCharacters = "['\"&]"; for (var i = 0; i < SpecialCharacters.length; i++) { if (str.indexOf(SpecialCharacters.charAt(i)) != -1) { return String.fromCharCode(SpecialCharacters.charCodeAt(i)); } } return ""; }

 

备注:

String.prototype动态地给String对象增加方法,上面的String.prototype.enHtml = function () {.....}就是;
下面是常用的方法:

// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{ 
    return this.replace(/[^\x00-\xff]/g,"**").length;
}
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith = function(str) 
{
    return this.substr(this.length - str.length) == str;
}
// 去掉字符左端的的空白字符
String.prototype.LeftTrim = function()
{
    return this.replace(/(^[\\s]*)/g, "");
}
// 去掉字符右端的空白字符
String.prototype.RightTrim = function()
{
    return this.replace(/([\\s]*$)/g, "");
}
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str) 
{
    return this.substr(0, str.length) == str;
}
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

 

转载于:https://www.cnblogs.com/wxh19860528/archive/2013/01/11/2856594.html

<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“AddNewUser.aspx.cs” Inherits=“Admin_lizhitong_AddNewUser” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=“http://www.w3.org/1999/xhtml”> <head runat=“server”> <title>注册新用户</title> <link href=“/css/Style.css” rel=“stylesheet” type=“text/css” /> </head> <body> <form id=“form1” runat=“server”> <div> <table border=“0” width=“100%” cellspacing=“1” cellpadding=“2” id=“table2” class=“tableBorder”> <tr> <th valign=“middle” colspan=“2” height=“25”> <table align=“center” width=“100%”> <tr> <td width=“96%” colspan=“2”> 注册用户 </td> </tr> </table> </th> </tr> <tr> <td width=“150” height=“47” align=“center” class=“forumRow”> 用户名: </td> <td class=“forumRow” align=“left”> <asp:TextBox ID=“tbYHM” runat=“server” Height=“19px” Width=“200px” spellcheck=“false” autocomplete=“off” aria-autocomplete=“none”></asp:TextBox> </td> </tr> <tr> <td width=“150” height=“47” align=“center” class=“forumRow”> 用户密码: </td> <td class=“forumRow” align=“left”> <asp:TextBox ID=“tbYHMM” runat=“server” Height=“19px” Width=“200px” spellcheck=“false” autocomplete=“off” aria-autocomplete=“none”></asp:TextBox> </td> </tr> <tr> <td width=“150” height=“47” align=“center” class=“forumRow”> 确认密码: </td> <td class=“forumRow” align=“left”> <asp:TextBox ID=“tbQRMM” runat=“server” Height=“19px” Width=“200px” spellcheck=“false” autocomplete=“off” aria-autocomplete=“none”></asp:TextBox> <asp:Literal ID=“mmErrorMessage” runat=“server”></asp:Literal> </td> </tr> <tr> <td width=“150” height=“47” align=“center” class=“forumRow”> 用户姓名: </td> <td class=“forumRow” align=“left”> <asp:TextBox ID=“tbYHXM” runat=“server” Height=“19px” Width=“200px” spellcheck=“false” autocomplete=“off” aria-autocomplete=“none”></asp:TextBox> </td> </tr> <tr> <td width=“80” height=“47” align=“center” class=“forumRow”> 所属区域: </td> <td class=“forumRow” align=“left”> <div class=“dropdown-container” style=“max-width: 450px;”> <asp:DropDownList ID=“ddlZJ” CssClass=“custom-dropdown” runat=“server” AutoPostBack=“True” onselectedindexchanged=“ddlZJ_SelectedIndexChanged”></asp:DropDownList> <asp:DropDownList ID=“ddlPCS” CssClass=“custom-dropdown” runat=“server” AutoPostBack=“True” onselectedindexchanged=“ddlPCS_SelectedIndexChanged”></asp:DropDownList> <asp:DropDownList ID=“ddlVillage” CssClass=“custom-dropdown” runat=“server” AutoPostBack=“True”></asp:DropDownList> </div> </td> </tr> <tr> <td width=“150” height=“47” align=“center” class=“forumRow”> 工作单位: </td> <td class=“forumRow” align=“left”> <asp:TextBox ID=“tbGZDW” runat=“server” Height=“19px” Width=“200px” spellcheck=“false” autocomplete=“off” aria-autocomplete=“none”></asp:TextBox> <td class=“forumRow” align=“left”> <asp:DropDownList ID=“ddlYHJS” CssClass=“custom-dropdown” style=“width:300px;” runat=“server”></asp:DropDownList> </td> </tr> <tr> <td valign="middle" colspan="2" align="center" class="forumRow"> <span style="color: Red; padding-left: 10px;"> <asp:Button ID="btSubmit" runat="server" Text="提交注册" OnClick="btSubmit_Click" />  <asp:Literal ID="ltMessage" runat="server"></asp:Literal></span> </td> </tr> </table> </div> </form> </body> </html> 怎么让tbQRMM和tbYHMM显示为*号,用TextMode="Password"方法
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值