注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一车,一房,一个温暖家

10万左右的车,80平方的房子,这是我奋斗的暖暖的家

 
 
 

日志

 
 

引用 自定义C#数据验证、安全、转换类   

2015-06-15 22:36:50|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

  自定义C#数据验证、安全、转换类 1.数据验证类


 程序代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

public class DataValidator
{
    /// <summary>
    /// 实数验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsDecimal(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return false;
        }
        return Regex.IsMatch(input, "^[0-9]+[.]?[0-9]+$");
    }

    /// <summary>
    /// 带符号的实数验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsDecimalSign(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return false;
        }
        return Regex.IsMatch(input, "^[+-]?[0-9]+[.]?[0-9]+$");
    }

    /// <summary>
    /// 正整数验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsNumber(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return false;
        }
        return Regex.IsMatch(input, "^[0-9]+$");
    }

    /// <summary>
    /// 整数验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsNumberSign(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return false;
        }
        return Regex.IsMatch(input, "^[+-]?[0-9]+$");
    }

    /// <summary>
    /// 邮编验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsPostCode(string input)
    {
        return (IsNumber(input) && (input.Length == 6));
    }

    /// <summary>
    /// 邮箱地址格式验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsEmail(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return false;
        }
        return Regex.IsMatch(input, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
    }

    /// <summary>
    /// IP地址验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsIP(string input)
    {
        return (!string.IsNullOrEmpty(input) && Regex.IsMatch(input.Trim(), @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"));
    }

    /// <summary>
    /// URL验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsUrl(string input)
    {
        if (string.IsNullOrEmpty(input))
        {
            return false;
        }
        return Regex.IsMatch(input, @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
    }

    /// <summary>
    /// 区号验证
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static bool IsAreaCode(string input)
    {
        return ((IsNumber(input) && (input.Length >= 3)) && (input.Length <= 5));
    }

    /// <summary>
    /// 用户名格式验证,长度[0,20],不能含有\\/\"[]:|<>+=;,?*@
    /// </summary>
    /// <param name="userName"></param>
    /// <returns></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 str = "\\/\"[]:|<>+=;,?*@";
        for (int i = 0; i < userName.Length; i++)
        {
            if (str.IndexOf(userName[i]) >= 0)
            {
                return false;
            }
        }
        return true;
    }
}

2.数据安全类


 程序代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

public class DataSecurity
{
    /// <summary>
    /// 特殊字符过滤
    /// </summary>
    /// <param name="strchar"></param>
    /// <returns></returns>
    public static string FilterBadChar(string strchar)
    {
        string input = "";
        if (string.IsNullOrEmpty(strchar))
        {
            return "";
        }
        string str = strchar;
        string[] strArray = new string[] {
                "+", "'", "%", "^", "&", "?", "(", ")", "<", ">", "[", "]", "{", "}", "/", "\"",
                ";", ":", "Chr(34)", "Chr(0)", "--"
             };
        StringBuilder builder = new StringBuilder(str);
        for (int i = 0; i < strArray.Length; i++)
        {
            input = builder.Replace(strArray[i], "").ToString();
        }
        return Regex.Replace(input, "@+", "@");
    }

    /// <summary>
    /// SQL注入过滤
    /// </summary>
    /// <param name="strchar"></param>
    /// <returns></returns>
    public static string FilterSqlKeyword(string strchar)
    {
        bool flag = false;
        if (string.IsNullOrEmpty(strchar))
        {
            return string.Empty;
        }
        strchar = strchar.ToLower();
        string[] strArray = new string[] {
                "select", "update", "insert", "delete", "declare", "@", "exec", "dbcc", "alter", "drop", "create", "backup", "if", "else", "end", "and",
                "or", "add", "set", "open", "close", "use", "begin", "retun", "as", "go", "exists", "kill"
             };
        for (int i = 0; i < strArray.Length; i++)
        {
            if (strchar.Contains(strArray[i]))
            {
                strchar = strchar.Replace(strArray[i], "");
                flag = true;
            }
        }
        if (flag)
        {
            return FilterSqlKeyword(strchar);
        }
        return strchar;
    }

    /// <summary>
    /// HTML编码
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string HtmlDecode(object value)
    {
        if (value == null)
        {
            return null;
        }
        return HtmlDecode(value.ToString());
    }

    /// <summary>
    /// HTML编码
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string HtmlDecode(string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            value = value.Replace("<br>", "\n");
            value = value.Replace("& gt;", ">");
            value = value.Replace("& lt;", "<");
            value = value.Replace("& nbsp;", " ");
            value = value.Replace("& #39;", "'");
            value = value.Replace("& quot;", "\"");
        }
        return value;
    }

    /// <summary>
    /// HTML解码
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string HtmlEncode(object value)
    {
        if (value == null)
        {
            return null;
        }
        return HtmlEncode(value.ToString());
    }

    /// <summary>
    /// HTML解码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string HtmlEncode(string str)
    {
        if (!string.IsNullOrEmpty(str))
        {
            str = str.Replace("<", "& lt;");
            str = str.Replace(">", "& gt;");
            str = str.Replace(" ", "& nbsp;");
            str = str.Replace("'", "& #39;");
            str = str.Replace("\"", "& quot;");
            str = str.Replace("\r\n", "<br>");
            str = str.Replace("\n", "<br>");
        }
        return str;
    }
}

3.数据转换类


 程序代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

public class DataConverter
{
    /// <summary>
    /// 日期转换
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static DateTime CDate(object input)
    {
        if (!Convert.IsDBNull(input) && !object.Equals(input, null))
        {
            return CDate(input.ToString());
        }
        return DateTime.Now;
    }

    /// <summary>
    /// 日期转换
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static DateTime CDate(string input)
    {
        DateTime now;
        if (!DateTime.TryParse(input, out now))
        {
            now = DateTime.Now;
        }
        return now;
    }

    /// <summary>
    /// 日期转换
    /// </summary>
    /// <param name="input"></param>
    /// <param name="outTime"></param>
    /// <returns></returns>
    public static DateTime? CDate(string input, DateTime? outTime)
    {
        DateTime time;
        if (!DateTime.TryParse(input, out time))
        {
            return outTime;
        }
        return new DateTime?(time);
    }

    /// <summary>
    /// 实数转换
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static decimal CDecimal(object input)
    {
        return CDecimal(input, 0M);
    }

    /// <summary>
    /// 实数转换
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static decimal CDecimal(string input)
    {
        return CDecimal(input, 0M);
    }

    /// <summary>
    /// 实数转换
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static decimal CDecimal(object input, decimal defaultValue)
    {
        if (!Convert.IsDBNull(input) && !object.Equals(input, null))
        {
            return CDecimal(input.ToString(), defaultValue);
        }
        return 0M;
    }

    /// <summary>
    /// 实数转换
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static decimal CDecimal(string input, decimal defaultValue)
    {
        decimal num;
        if (!decimal.TryParse(input, out num))
        {
            num = defaultValue;
        }
        return num;
    }

    /// <summary>
    /// 双精
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static double CDouble(object input)
    {
        return CDouble(input, 0.0);
    }

    /// <summary>
    /// 双精
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static double CDouble(string input)
    {
        return CDouble(input, 0.0);
    }

    /// <summary>
    /// 双精
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static double CDouble(object input, double defaultValue)
    {
        if (!Convert.IsDBNull(input) && !object.Equals(input, null))
        {
            return CDouble(input.ToString(), defaultValue);
        }
        return 0.0;
    }

    /// <summary>
    /// 双精
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static double CDouble(string input, double defaultValue)
    {
        double num;
        if (!double.TryParse(input, out num))
        {
            return defaultValue;
        }
        return num;
    }

    /// <summary>
    /// 整型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static int CInt(object input)
    {
        return CInt(input, 0);
    }

    /// <summary>
    /// 整型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static int CInt(string input)
    {
        return CInt(input, 0);
    }

    /// <summary>
    /// 整型
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static int CInt(object input, int defaultValue)
    {
        if (!Convert.IsDBNull(input) && !object.Equals(input, null))
        {
            return CInt(input.ToString(), defaultValue);
        }
        return defaultValue;
    }

    /// <summary>
    /// 整型
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static int CInt(string input, int defaultValue)
    {
        int num;
        if (!int.TryParse(input, out num))
        {
            num = defaultValue;
        }
        return num;
    }

    /// <summary>
    /// 单精
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static float CFloat(object input)
    {
        return CFloat(input, 0f);
    }

    /// <summary>
    /// 单精
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static float CFloat(string input)
    {
        return CFloat(input, 0f);
    }

    /// <summary>
    /// 单精
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static float CFloat(object input, float defaultValue)
    {
        if (!Convert.IsDBNull(input) && !object.Equals(input, null))
        {
            return CFloat(input.ToString(), defaultValue);
        }
        return 0f;
    }

    /// <summary>
    /// 单精
    /// </summary>
    /// <param name="input"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static float CFloat(string input, float defaultValue)
    {
        float num;
        if (!float.TryParse(input, out num))
        {
            num = defaultValue;
        }
        return num;
    }

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/iloveyoupk/archive/2008/12/08/3479092.aspx

  评论这张
 
阅读(4)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018