xss攻击前后台防范

 

前端

$(function () {
    $("input.specialChar").on("blur", function () {
        var _txt = $(this).val();
    })
    $("input.specialChar").on("keyup", function () {
        handlerInput(this);
    });
    $("input.specialChar").on("beforepaste", function () {
        handlerInput(this);
    });
    function handlerInput(element) {
        var _txt = $(element).val();

          //$(element).val(_txt.replace(/["'<>%;)(&+]/g, ""));
          $(element).val(_txt.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, ""));//只能输入汉字、数字和字母

    }
})

后台model层

/// <summary>
        /// 成本名称
        /// </summary>
        public string cCostType
        {
            set
            {
                _ccosttype = value;
                _ccosttype = System.Text.RegularExpressions.Regex.Replace(_ccosttype, @"[^\u4e00-\u9fa5a-zA-Z0-9]*", @"");//去掉特殊字符,只能输入汉字、字母和数字
                _ccosttype = System.Web.HttpUtility.HtmlDecode(_ccosttype);//转换HTML编码

            }
            get { return System.Web.HttpUtility.HtmlEncode(_ccosttype); }//解析HTML编码
        }

 

posted @ 2016-10-26 18:56  wjl910  阅读(343)  评论(0)    收藏  举报