zoukankan      html  css  js  c++  java
  • 服务端验证

    服务端验证代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Common.Extensions
    {
        using System.Text.RegularExpressions;
    
        /// <summary>
        /// 系统数据验证类
        /// </summary>
        public abstract class DataValidator
        {
            /// <summary>
            /// 检测字符串是否为数字
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为数字,则为 true;否则为 false。</returns>
            public static bool IsNumber(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, "^[0-9]+$");
                }
            }
    
            /// <summary>
            /// 检测字符串是否为数字和,分隔符
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为数字,则为 true;否则为 false。</returns>
            public static bool IsNumbers(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, "^[0-9,]+$");
                }
            }
    
            /// <summary>
            /// 判断字符串是否是有效的IP地址
            /// </summary>
            /// <param name="input">IP地址字符串</param>
            /// <returns>有效IP地址返回true ;否则返回false</returns>
            public static bool IsIP(string input)
            {
                if (!string.IsNullOrEmpty(input))
                {
                    return Regex.IsMatch(input.Trim(), @"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$");
                }
                else
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 检测字符串是否为数字,可带正负号
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为数字,则为 true;否则为 false。</returns>
            public static bool IsNumberSign(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, "^[+-]?[0-9]+$");
                }
            }
    
            /// <summary>
            /// 检测字符串是否为浮点数
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为浮点数,则为 true;否则为 false。</returns>
            public static bool IsDecimal(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, @"^[0-9]+(.[0-9]+)?$");
                }
            }
    
            /// <summary>
            /// 检测字符串是否为浮点数 可带正负号
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为浮点数,则为 true;否则为 false。</returns>
            public static bool IsDecimalSign(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, @"^[+-]?[0-9]+(.[0-9]+)?$");
                }
            }
    
            /// <summary>
            /// 检测字符串是否为有效的URL地址
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为有效的URL地址,则为 true;否则为 false。</returns>
            public static bool IsUrl(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, @"^http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?$", RegexOptions.IgnoreCase);
                }
            }
    
            /// <summary>
            /// 检测字符串是否为有效的邮件地址
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为有效的邮件地址,则为 true;否则为 false。</returns>
            public static bool IsEmail(string input)
            {
                if (string.IsNullOrEmpty(input))
                {
                    return false;
                }
                else
                {
                    return Regex.IsMatch(input, @"^w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*$");
                }
            }
    
            /// <summary>
            /// 检测字符串是否为有效的邮政编码
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为有效的邮政编码,则为 true;否则为 false。</returns>
            public static bool IsPostCode(string input)
            {
                if (!IsNumber(input) || input.Length != 6)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
    
            /// <summary>
            /// 检测字符串是否为有效的区号
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为有效的区号,则为 true;否则为 false。</returns>
            public static bool IsAreaCode(string input)
            {
                if (!IsNumber(input) || input.Length < 3 || input.Length > 5)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
    
            /// <summary>
            /// 检测字符串是否为有效的ID
            /// </summary>
            /// <param name="input">需要检查的字符串</param>
            /// <returns>如果字符串为有效的ID,则为 true;否则为 false。</returns>
            public static bool IsValidId(string input)
            {
                bool valid;
                if (string.IsNullOrEmpty(input))
                {
                    valid = false;
                }
                else
                {
                    input = input.Replace("|", string.Empty).Replace(",", string.Empty).Replace("-", string.Empty).Replace(" ", string.Empty).Trim();
                    if (string.IsNullOrEmpty(input))
                    {
                        valid = false;
                    }
                    else
                    {
                        if (IsNumber(input))
                        {
                            valid = true;
                        }
                        else
                        {
                            valid = false;
                        }
                    }
                }
    
                return valid;
            }
    
            /// <summary>
            /// 验证是否符合用户名规则
            /// </summary>
            /// <param name="userName">用户名</param>
            /// <returns>符合返回true,不符合返回false</returns>
            public static bool IsValidUserName(string userName)
            {
                if (string.IsNullOrEmpty(userName))
                {
                    return false;
                }
    
                if (userName.Length > 20)
                {
                    return false;
                }
    
                if (userName.Trim().Length == 0)
                {
                    return false;
                }
    
                if (userName.Trim(new char[] { '.' }).Length == 0)
                {
                    return false;
                }
    
                string notContains = @"/""[]:|<>+=;,?*@";
                for (int i = 0; i < userName.Length; i++)
                {
                    if (notContains.IndexOf(userName[i]) >= 0)
                    {
                        return false;
                    }
                }
    
                return true;
            }
    
            /// <summary>
            /// 过滤掉字符串中会引起注入攻击的字符
            /// </summary>
            /// <param name="strchar">要过滤的字符串</param>
            /// <returns>已过滤的字符串</returns>
            public static string FilterBadChar(string strchar)
            {
                string tempstrChar;
                string newstrChar = string.Empty;
                if (string.IsNullOrEmpty(strchar))
                {
                    newstrChar = string.Empty;
                }
                else
                {
                    tempstrChar = strchar;
                    string[] strBadChar = { "+", "'", "%", "^", "&", "?", "(", ")", "<", ">", "[", "]", "{", "}", "/", """, ";", ":", "Chr(34)", "Chr(0)", "--" };
                    StringBuilder strBuilder = new StringBuilder(tempstrChar);
                    for (int i = 0; i < strBadChar.Length; i++)
                    {
                        newstrChar = strBuilder.Replace(strBadChar[i], string.Empty).ToString();
                    }
    
                    newstrChar = Regex.Replace(newstrChar, "@+", "@");
                }
    
                return newstrChar;
            }
    
            /// <summary>
            /// 过滤sql语句中like的内容
            /// </summary>
            /// <param name="strchar">like的内容</param>
            /// <returns>返回过滤后sql语句中like的内容</returns>
            public static string FilterLikeSql(string strchar)
            {
                string tempstrChar;
                string newstrChar = string.Empty;
                if (string.IsNullOrEmpty(strchar))
                {
                    newstrChar = string.Empty;
                }
                else
                {
                    tempstrChar = strchar;
                    string[] strBadChar = { "'", "%" };
                    StringBuilder strBuilder = new StringBuilder(tempstrChar);
                    for (int i = 0; i < strBadChar.Length; i++)
                    {
                        newstrChar = strBuilder.Replace(strBadChar[i], "\" + strBadChar[i]).ToString();
                    }
                }
    
                return newstrChar;
    
            }
    
            /// <summary>
            /// 过滤字符串中换行空格
            /// </summary>
            /// <param name="strchar">字符串</param>
            /// <returns>返回过滤后过滤字符内容</returns>
            public static string FilterStringLineBr(string strchar)
            {
                string newstrChar = string.Empty;
                if (string.IsNullOrEmpty(strchar))
                {
                    newstrChar = string.Empty;
                }
                else
                {
                    newstrChar = strchar.Replace("
    ", "").Replace("
    ", "").Replace("
    ", "");
                }
    
                return newstrChar;
            }
    
        }
    }
    

      

  • 相关阅读:
    编译原理是什么?有什么用?
    词频统计
    python基础综合练习
    熟悉常用的Linux操作
    大数据概述
    c语言程序的文法分析
    词法分析实验报告
    未完成的词法分析
    一名初学者对编译原理的看法
    使用Bootstrap.css 中IE下页面加载过慢问题
  • 原文地址:https://www.cnblogs.com/kksguijiao/p/4727148.html
Copyright © 2011-2022 走看看