NET-页面数据校验通用类

代码
  1      public   class  PageValidate
  2 ExpandedBlockStart.gifContractedBlock.gif     {
  3        private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
  4        private static Regex RegNumber = new Regex("^[0-9]+$");
  5        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
  6        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
  7        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
  8        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
  9        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
 10
 11        public PageValidate()
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 13        }

 14
 15
 16ContractedSubBlock.gifExpandedSubBlockStart.gif        数字字符串检查#region 数字字符串检查        
 17        public static bool IsPhone(string inputData)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 19            Match m = RegPhone.Match(inputData);
 20            return m.Success;
 21        }

 22ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 23        /// 检查Request查询字符串的键值,是否是数字,最大长度限制
 24        /// </summary>
 25        /// <param name="req">Request</param>
 26        /// <param name="inputKey">Request的键值</param>
 27        /// <param name="maxLen">最大长度</param>
 28        /// <returns>返回Request查询字符串</returns>

 29        public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 31            string retVal = string.Empty;
 32            if(inputKey != null && inputKey != string.Empty)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 34                retVal = req.QueryString[inputKey];
 35                if(null == retVal)
 36                    retVal = req.Form[inputKey];
 37                if(null != retVal)
 38ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 39                    retVal = SqlText(retVal, maxLen);
 40                    if(!IsNumber(retVal))
 41                        retVal = string.Empty;
 42                }

 43            }

 44            if(retVal == null)
 45                retVal = string.Empty;
 46            return retVal;
 47        }
        
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 49        /// 是否数字字符串
 50        /// </summary>
 51        /// <param name="inputData">输入字符串</param>
 52        /// <returns></returns>

 53        public static bool IsNumber(string inputData)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 55            Match m = RegNumber.Match(inputData);
 56            return m.Success;
 57        }

 58
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 60        /// 是否数字字符串 可带正负号
 61        /// </summary>
 62        /// <param name="inputData">输入字符串</param>
 63        /// <returns></returns>

 64        public static bool IsNumberSign(string inputData)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 66            Match m = RegNumberSign.Match(inputData);
 67            return m.Success;
 68        }
        
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 70        /// 是否是浮点数
 71        /// </summary>
 72        /// <param name="inputData">输入字符串</param>
 73        /// <returns></returns>

 74        public static bool IsDecimal(string inputData)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 76            Match m = RegDecimal.Match(inputData);
 77            return m.Success;
 78        }
        
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 80        /// 是否是浮点数 可带正负号
 81        /// </summary>
 82        /// <param name="inputData">输入字符串</param>
 83        /// <returns></returns>

 84        public static bool IsDecimalSign(string inputData)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 86            Match m = RegDecimalSign.Match(inputData);
 87            return m.Success;
 88        }
        
 89
 90        #endregion

 91
 92ContractedSubBlock.gifExpandedSubBlockStart.gif        中文检测#region 中文检测
 93
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 95        /// 检测是否有中文字符
 96        /// </summary>
 97        /// <param name="inputData"></param>
 98        /// <returns></returns>

 99        public static bool IsHasCHZN(string inputData)
100ExpandedSubBlockStart.gifContractedSubBlock.gif        {
101            Match m = RegCHZN.Match(inputData);
102            return m.Success;
103        }
    
104
105        #endregion

106
107ContractedSubBlock.gifExpandedSubBlockStart.gif        邮件地址#region 邮件地址
108ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
109        /// 是否是浮点数 可带正负号
110        /// </summary>
111        /// <param name="inputData">输入字符串</param>
112        /// <returns></returns>

113        public static bool IsEmail(string inputData)
114ExpandedSubBlockStart.gifContractedSubBlock.gif        {
115            Match m = RegEmail.Match(inputData);
116            return m.Success;
117        }
        
118
119        #endregion

120
121ContractedSubBlock.gifExpandedSubBlockStart.gif        其他#region 其他
122
123ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
124        /// 检查字符串最大长度,返回指定长度的串
125        /// </summary>
126        /// <param name="sqlInput">输入字符串</param>
127        /// <param name="maxLength">最大长度</param>
128        /// <returns></returns>            

129        public static string SqlText(string sqlInput, int maxLength)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        {            
131            if(sqlInput != null && sqlInput != string.Empty)
132ExpandedSubBlockStart.gifContractedSubBlock.gif            {
133                sqlInput = sqlInput.Trim();                            
134                if(sqlInput.Length > maxLength)//按最大长度截取字符串
135                    sqlInput = sqlInput.Substring(0, maxLength);
136            }

137            return sqlInput;
138        }
        
139ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
140        /// 字符串编码
141        /// </summary>
142        /// <param name="inputData"></param>
143        /// <returns></returns>

144        public static string HtmlEncode(string inputData)
145ExpandedSubBlockStart.gifContractedSubBlock.gif        {
146            return HttpUtility.HtmlEncode(inputData);
147        }

148ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
149        /// 设置Label显示Encode的字符串
150        /// </summary>
151        /// <param name="lbl"></param>
152        /// <param name="txtInput"></param>

153        public static void SetLabel(Label lbl, string txtInput)
154ExpandedSubBlockStart.gifContractedSubBlock.gif        {
155            lbl.Text = HtmlEncode(txtInput);
156        }

157        public static void SetLabel(Label lbl, object inputObj)
158ExpandedSubBlockStart.gifContractedSubBlock.gif        {
159            SetLabel(lbl, inputObj.ToString());
160        }
        
161        //字符串清理
162        public static string InputText(string inputString, int maxLength)
163ExpandedSubBlockStart.gifContractedSubBlock.gif        {            
164            StringBuilder retVal = new StringBuilder();
165
166            // 检查是否为空
167            if ((inputString != null&& (inputString != String.Empty))
168ExpandedSubBlockStart.gifContractedSubBlock.gif            {
169                inputString = inputString.Trim();
170                
171                //检查长度
172                if (inputString.Length > maxLength)
173                    inputString = inputString.Substring(0, maxLength);
174                
175                //替换危险字符
176                for (int i = 0; i < inputString.Length; i++)
177ExpandedSubBlockStart.gifContractedSubBlock.gif                {
178                    switch (inputString[i])
179ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
180                        case '"':
181                            retVal.Append("&quot;");
182                            break;
183                        case '<':
184                            retVal.Append("&lt;");
185                            break;
186                        case '>':
187                            retVal.Append("&gt;");
188                            break;
189                        default:
190                            retVal.Append(inputString[i]);
191                            break;
192                    }

193                }
                
194                retVal.Replace("'"" ");// 替换单引号
195            }

196            return retVal.ToString();
197            
198        }

199ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
200        /// 转换成 HTML code
201        /// </summary>
202        /// <param name="str">string</param>
203        /// <returns>string</returns>

204        public static string Encode(string str)
205ExpandedSubBlockStart.gifContractedSubBlock.gif        {            
206            str = str.Replace("&","&amp;");
207            str = str.Replace("'","''");
208            str = str.Replace("\"","&quot;");
209            str = str.Replace(" ","&nbsp;");
210            str = str.Replace("<","&lt;");
211            str = str.Replace(">","&gt;");
212            str = str.Replace("\n","<br>");
213            return str;
214        }

215ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
216        ///解析html成 普通文本
217        /// </summary>
218        /// <param name="str">string</param>
219        /// <returns>string</returns>

220        public static string Decode(string str)
221ExpandedSubBlockStart.gifContractedSubBlock.gif        {            
222            str = str.Replace("<br>","\n");
223            str = str.Replace("&gt;",">");
224            str = str.Replace("&lt;","<");
225            str = str.Replace("&nbsp;"," ");
226            str = str.Replace("&quot;","\"");
227            return str;
228        }

229
230        public static string SqlTextClear(string sqlText)
231ExpandedSubBlockStart.gifContractedSubBlock.gif        {
232            if (sqlText == null)
233ExpandedSubBlockStart.gifContractedSubBlock.gif            {
234                return null;
235            }

236            if (sqlText == "")
237ExpandedSubBlockStart.gifContractedSubBlock.gif            {
238                return "";
239            }

240            sqlText = sqlText.Replace(",""");//去除,
241            sqlText = sqlText.Replace("<""");//去除<
242            sqlText = sqlText.Replace(">""");//去除>
243            sqlText = sqlText.Replace("--""");//去除--
244            sqlText = sqlText.Replace("'""");//去除'
245            sqlText = sqlText.Replace("\"""");//去除"
246            sqlText = sqlText.Replace("=""");//去除=
247            sqlText = sqlText.Replace("%""");//去除%
248            sqlText = sqlText.Replace(" """);//去除空格
249            return sqlText;
250        }

251        #endregion

252
253ContractedSubBlock.gifExpandedSubBlockStart.gif        是否由特定字符组成#region 是否由特定字符组成
254        public static bool isContainSameChar(string strInput)
255ExpandedSubBlockStart.gifContractedSubBlock.gif        {
256            string charInput = string.Empty;
257            if (!string.IsNullOrEmpty(strInput))
258ExpandedSubBlockStart.gifContractedSubBlock.gif            {
259                charInput = strInput.Substring(01);
260            }

261            return isContainSameChar(strInput, charInput, strInput.Length);
262        }

263
264        public static bool isContainSameChar(string strInput, string charInput, int lenInput)
265ExpandedSubBlockStart.gifContractedSubBlock.gif        {
266            if (string.IsNullOrEmpty(charInput))
267ExpandedSubBlockStart.gifContractedSubBlock.gif            {
268                return false;
269            }

270            else
271ExpandedSubBlockStart.gifContractedSubBlock.gif            {
272                Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
273                //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
274                Match m = RegNumber.Match(strInput);
275                return m.Success;
276            }

277        }

278        #endregion

279
280ContractedSubBlock.gifExpandedSubBlockStart.gif        检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查#region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
281ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
282        /// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
283        /// </summary>

284        public static bool isContainSpecChar(string strInput)
285ExpandedSubBlockStart.gifContractedSubBlock.gif        {
286ExpandedSubBlockStart.gifContractedSubBlock.gif            string[] list = new string[] "123456""654321" };
287            bool result = new bool();
288            for (int i = 0; i < list.Length; i++)
289ExpandedSubBlockStart.gifContractedSubBlock.gif            {
290                if (strInput == list[i])
291ExpandedSubBlockStart.gifContractedSubBlock.gif                {
292                    result = true;
293                    break;
294                }

295            }

296            return result;
297        }

298        #endregion

299    }

300

转载于:https://www.cnblogs.com/homezzm/archive/2009/11/27/1612071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值