欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现对用户输入数据进行校验的类实例

程序员文章站 2023-12-01 15:45:46
本文实例讲述了c#实现对用户输入数据进行校验的类。分享给大家供大家参考。具体如下: 这个c#类包含了各种常用数据验证的函数,包含验证是否是数字,校验email格式,区分中...

本文实例讲述了c#实现对用户输入数据进行校验的类。分享给大家供大家参考。具体如下:

这个c#类包含了各种常用数据验证的函数,包含验证是否是数字,校验email格式,区分中英文截取字符串,区分中英文计算字符串长度,检测是否包含中文字符,判断是否包含特定字符等

using system;
using system.text;
using system.web;
using system.web.ui.webcontrols;
using system.text.regularexpressions;
namespace dotnet.utilities
{
  public class pagevalidate
  {
    private static regex regnumber = new regex("^[0-9]+$");
    private static regex regnumbersign = new regex("^[+-]?[0-9]+$");
    private static regex regdecimal = new regex("^[0-9]+[.]?[0-9]+$");
    private static regex regdecimalsign = new regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
    private static regex regemail = new regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-za-z0-9] 语法一样
    private static regex regchzn = new regex("[\u4e00-\u9fa5]");
    public pagevalidate()
    {
    }
 
    #region 数字字符串检查
    /// <summary>
    /// 格式化字符串
    /// </summary>
    /// <param name="inputdata">源字符串</param>
    /// <param name="formatlevel">0:不做验证| 1:sql语句参数| 2:存储过程参数| 3:encodehtml| 4:encode+sql| 5:encode+存储过程</param>
    /// <returns>返回格式化后的字符串</returns>
    public static string formatstring(string inputdata, int formatlevel)
    {
      return inputdata;
    }
    /// <summary>
    /// 检查request查询字符串的键值,是否是数字,最大长度限制
    /// </summary>
    /// <param name="req">request</param>
    /// <param name="inputkey">request的键值</param>
    /// <param name="maxlen">最大长度</param>
    /// <returns>返回request查询字符串</returns>
    public static string fetchinputdigit(httprequest req, string inputkey, int maxlen)
    {
      string retval = string.empty;
      if (inputkey != null && inputkey != string.empty)
      {
        retval = req.querystring[inputkey];
        if (null == retval)
          retval = req.form[inputkey];
        if (null != retval)
        {
          retval = sqltext(retval, maxlen);
          if (!isnumber(retval))
            retval = string.empty;
        }
      }
      if (retval == null)
        retval = string.empty;
      return retval;
    }
    public enum checktype
    { none, int, signint, float, signfloat, chinese, mail }
    /// <summary>
    /// 检测字符串类型
    /// </summary>
    /// <param name="inputdata">输入字符串</param>
    /// <param name="checktype">0:不检测| 1:数字| 2:符号数字| 3: 浮点数| 4:符号浮点| 5: 中文?| 6:邮件?</param>
    /// <returns></returns>
    public static bool checkstring(string inputdata, int checktype)
    {
      bool _return = false;
      switch (checktype)
      {
        case 0:
          _return = true;
          break;
        case 1:
          _return = isnumber(inputdata);
          break;
        case 2:
          _return = isnumbersign(inputdata);
          break;
        case 3:
          _return = isdecimal(inputdata);
          break;
        case 4:
          _return = isdecimalsign(inputdata);
          break;
        case 5:
          _return = ishaschzn(inputdata);
          break;
        case 6:
          _return = isemail(inputdata);
          break;
        default:
          _return = false;
          break;
      }
      return _return;
    }
    /// <summary>
    /// 是否数字字符串
    /// </summary>
    /// <param name="inputdata">输入字符串</param>
    /// <returns></returns>
    public static bool isnumber(string inputdata)
    {
      match m = regnumber.match(inputdata);
      return m.success;
    }
    /// <summary>
    /// 是否数字字符串 可带正负号
    /// </summary>
    /// <param name="inputdata">输入字符串</param>
    /// <returns></returns>
    public static bool isnumbersign(string inputdata)
    {
      match m = regnumbersign.match(inputdata);
      return m.success;
    }
    /// <summary>
    /// 是否是浮点数
    /// </summary>
    /// <param name="inputdata">输入字符串</param>
    /// <returns></returns>
    public static bool isdecimal(string inputdata)
    {
      match m = regdecimal.match(inputdata);
      return m.success;
    }
    /// <summary>
    /// 是否是浮点数 可带正负号
    /// </summary>
    /// <param name="inputdata">输入字符串</param>
    /// <returns></returns>
    public static bool isdecimalsign(string inputdata)
    {
      match m = regdecimalsign.match(inputdata);
      return m.success;
    }
    #endregion
    #region 中文检测
    /// <summary>
    /// 检测是否有中文字符
    /// </summary>
    /// <param name="inputdata"></param>
    /// <returns></returns>
    public static bool ishaschzn(string inputdata)
    {
      match m = regchzn.match(inputdata);
      return m.success;
    }
    #endregion
    public static string getshortdate(string dt)
    {
      return convert.todatetime(dt).toshortdatestring();
    }
    #region 邮件地址
    /// <summary>
    /// 是否是浮点数 可带正负号
    /// </summary>
    /// <param name="inputdata">输入字符串</param>
    /// <returns></returns>
    public static bool isemail(string inputdata)
    {
      match m = regemail.match(inputdata);
      return m.success;
    }
    #endregion
    #region 其他
    /// <summary>
    /// 检查字符串最大长度,返回指定长度的串
    /// </summary>
    /// <param name="sqlinput">输入字符串</param>
    /// <param name="maxlength">最大长度</param>
    /// <returns></returns>    
    public static string sqltext(string sqlinput, int maxlength)
    {
      if (sqlinput != null && sqlinput != string.empty)
      {
        sqlinput = sqlinput.trim();
        if (sqlinput.length > maxlength)//按最大长度截取字符串
          sqlinput = sqlinput.substring(0, maxlength);
      }
      return sqlinput;
    }
 
    /// <summary>
    /// 字符串编码
    /// </summary>
    /// <param name="inputdata"></param>
    /// <returns></returns>
    public static string htmlencode(string inputdata)
    {
      return httputility.htmlencode(inputdata);
    }
    /// <summary>
    /// 设置label显示encode的字符串
    /// </summary>
    /// <param name="lbl"></param>
    /// <param name="txtinput"></param>
    public static void setlabel(label lbl, string txtinput)
    {
      lbl.text = htmlencode(txtinput);
    }
    public static void setlabel(label lbl, object inputobj)
    {
      setlabel(lbl, inputobj.tostring());
    }
    #endregion
    #region 对于用户权限从数据库中读出的解密过程
    public static string switch_riddle(string s_ch)//解密
    {
      string s_out, s_temp, temp;
      int i_len = s_ch.length;
      if (i_len == 0 || s_ch == "")
      {
        s_out = "0";
      }
      temp = "";
      s_temp = "";
      s_out = "";
      for (int i = 0; i <= i_len - 1; i++)
      {
        temp = s_ch.substring(i, 1);
        switch (temp)
        {
          case "a": s_temp = "1010";
            break;
          case "b": s_temp = "1011";
            break;
          case "c": s_temp = "1100";
            break;
          case "d": s_temp = "1101";
            break;
          case "e": s_temp = "1110";
            break;
          case "f": s_temp = "1111";
            break;
          case "0": s_temp = "0000";
            break;
          case "1": s_temp = "0001";
            break;
          case "2": s_temp = "0010";
            break;
          case "3": s_temp = "0011";
            break;
          case "4": s_temp = "0100";
            break;
          case "5": s_temp = "0101";
            break;
          case "6": s_temp = "0110";
            break;
          case "7": s_temp = "0111";
            break;
          case "8": s_temp = "1000";
            break;
          case "9": s_temp = "1001";
            break;
          default: s_temp = "0000";
            break;
        }
        s_out = s_out + s_temp;
        s_temp = "";
      }
      return s_out;
    }
    #endregion
    #region 用户权限的加密过程
    public static string switch_encrypt(string s_ch)
    {
      string s_out, s_temp, temp;
      int i_len = 64;
      if (i_len == 0 || s_ch == "")
      {
        s_out = "0000";
      }
      temp = "";
      s_temp = "";
      s_out = "";
      for (int i = 0; i <= i_len - 1; i = i + 4)
      {
        temp = s_ch.substring(i, 4);
        switch (temp)
        {
          case "1010": s_temp = "a";
            break;
          case "1011": s_temp = "b";
            break;
          case "1100": s_temp = "c";
            break;
          case "1101": s_temp = "d";
            break;
          case "1110": s_temp = "e";
            break;
          case "1111": s_temp = "f";
            break;
          case "0000": s_temp = "0";
            break;
          case "0001": s_temp = "1";
            break;
          case "0010": s_temp = "2";
            break;
          case "0011": s_temp = "3";
            break;
          case "0100": s_temp = "4";
            break;
          case "0101": s_temp = "5";
            break;
          case "0110": s_temp = "6";
            break;
          case "0111": s_temp = "7";
            break;
          case "1000": s_temp = "8";
            break;
          case "1001": s_temp = "9";
            break;
          default: s_temp = "0";
            break;
        }
        s_out = s_out + s_temp;
        s_temp = "";
      }
      return s_out;
    }//加密
    #endregion
    #region  访问权限
    public static bool checktrue(string s_admin, int a)
    {
      string s_temp = "";
      s_temp = s_admin.substring(a - 1, 1);  //s_admin为全局变量
      if (s_temp == "" || s_temp == "1")
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    #endregion
    #region  检测字符串长度
    /// <summary> 
    /// 计算文本长度,区分中英文字符,中文算两个长度,英文算一个长度
    /// </summary>
    /// <param name="text">需计算长度的字符串</param>
    /// <returns>int</returns>
    public static int text_length(string text)
    {
      int len = 0;
      for (int i = 0; i < text.length; i++)
      {
        byte[] byte_len = encoding.default.getbytes(text.substring(i, 1));
        if (byte_len.length > 1)
          len += 2; //如果长度大于1,是中文,占两个字节,+2
        else
          len += 1; //如果长度等于1,是英文,占一个字节,+1
      }
      return len;
    }
    #endregion
    #region  字符串长度区分中英文截取
    /// <summary> 
    /// 截取文本,区分中英文字符,中文算两个长度,英文算一个长度
    /// </summary>
    /// <param name="str">待截取的字符串</param>
    /// <param name="length">需计算长度的字符串</param>
    /// <returns>string</returns>
    public static string getsubstring(string str, int length)
    {
      string temp = str;
      int j = 0;
      int k = 0;
      for (int i = 0; i < temp.length; i++)
      {
        if (regex.ismatch(temp.substring(i, 1), @"[\u4e00-\u9fa5]+"))
        {
          j += 2;
        }
        else
        {
          j += 1;
        }
        if (j <= length)
        {
          k += 1;
        }
        if (j > length)
        {
          return temp.substring(0, k) + "..";
        }
      }
      return temp;
    }
    #endregion
    #region 页面html格式化
    public static string gethtml(string sdetail)
    {
      regex r;
      match m;
      #region 处理空格
      sdetail = sdetail.replace(" ", " ");
      #endregion
      #region 处理单引号
      sdetail = sdetail.replace("'", "'");
      #endregion
      #region 处理双引号
      sdetail = sdetail.replace("\"", """);
      #endregion
      #region html标记符
      sdetail = sdetail.replace("<", "<");
      sdetail = sdetail.replace(">", ">");
      #endregion
      #region 处理换行
      //处理换行,在每个新行的前面添加两个全角空格
      r = new regex(@"(\r\n(( )| )+)(?<正文>\s+)", regexoptions.ignorecase);
      for (m = r.match(sdetail); m.success; m = m.nextmatch())
      {
        sdetail = sdetail.replace(m.groups[0].tostring(), "<br>  " + m.groups["正文"].tostring());
      }
      //处理换行,在每个新行的前面添加两个全角空格
      sdetail = sdetail.replace("\r\n", "<br>");
      #endregion
      return sdetail;
    }
    #endregion
    #region 分页
    //public static string paging(string url, string para, int sumpage, int page)
    //{
    //  string result = string.empty;
    //  if (sumpage == 1)
    //  {
    //    return result;
    //  }
    //  if (page > sumpage)
    //  {
    //    page = 1;
    //  }
    //  if (sumpage > 0)
    //  {
    //    for (int i = 1; i <= sumpage; i++)
    //    {
    //      if (i == page)
    //      {
    //        result += string.format("<a class=\"a_page\" href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, i.tostring(), para, i.tostring() });
    //      }
    //      else
    //      {
    //        result += string.format("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, i.tostring(), para, i.tostring() });
    //      }
    //    }
    //  }
    //  return result;
    //}
    public static string paging(string url, string para, int sumpage, int page)
    {
      string result = string.empty;
      if (sumpage == 1)
      {
        return result;
      }
      if (sumpage > 500)
      {
        sumpage = 500;
      }
      if (page > sumpage)
      {
        page = 1;
      }
      stringbuilder sb = new stringbuilder();
      if (sumpage > 0)
      {
        switch (page)
        {
          case 1:
            sb.append(string.format("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page + 1, para, "下一页" }));
            break;
          default:
            if (sumpage == page)
            {
              sb.append(string.format("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page - 1, para, "上一页" }));
            }
            else
            {
              sb.append(string.format("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> <a href=\"{4}?page={5}{6}\">{7}</a> ",
                new object[] { url, page + 1, para, "下一页", url, page - 1, para, "上一页" }));
            }
            break;
        }
        sb.append(string.format("第{0}/{1}页</p>", new object[] { page, sumpage }));
      }
      return sb.tostring();
    }
    public static string paging(string url, string para, int sumpage, int page, system.web.ui.usercontrol mypaging)
    {
      mypaging.visible = false;
      string result = string.empty;
      if (sumpage == 1)
      {
        return result;
      }
      if (sumpage > 500)
      {
        sumpage = 500;
      }
      if (page > sumpage)
      {
        page = 1;
      }
      stringbuilder sb = new stringbuilder();
      if (sumpage > 0)
      {
        mypaging.visible = true;
        switch (page)
        {
          case 1:
            sb.append(string.format("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page + 1, para, "下一页" }));
            break;
          default:
            if (sumpage == page)
            {
              sb.append(string.format("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page - 1, para, "上一页" }));
            }
            else
            {
              sb.append(string.format("<a href=\"{0}?page={1}{2}\">{3}</a> <a href=\"{4}?page={5}{6}\">{7}</a> ",
                new object[] { url, page + 1, para, "下一页", url, page - 1, para, "上一页" }));
            }
            break;
        }
        sb.append(string.format("第{0}/{1}页", new object[] { page, sumpage }));
      }
      return sb.tostring();
    }
    public static string paging(string para, int sumpage, int page, int count)
    {
      string result = string.empty;
      if (page > sumpage)
      {
        page = 1;
      }
      stringbuilder sb = new stringbuilder();
      if (sumpage > 0)
      {
        if (sumpage != 1)
        {
          switch (page)
          {
            case 1:
              sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "下一页" }));
              break;
            default:
              if (sumpage == page)
              {
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a> ", new object[] { page - 1, para, "上一页" }));
              }
              else
              {
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a> <a href=\"?page={3}{4}\">{5}</a> ",
                  new object[] { page - 1, para, "上一页", page + 1, para, "下一页" }));
              }
              break;
          }
        }
        sb.append(string.format("第{0}/{1}页 共{2}条", new object[] { page, sumpage, count }));
      }
      return sb.tostring();
    }
    public static void paging(string clinktail, int sumpage, int page, system.web.ui.webcontrols.label page_view)
    {
      if (sumpage > 0)
      {
        int n = sumpage;  //总页数
        int x = page;  //得到当前页
        int i;
        int endpage;
        string pageview = "", pageviewtop = "";
        if (x > 1)
        {
          pageview += "  <a class='pl' href='?page=1" + clinktail + "'>第1页</a> | ";
          pageviewtop += "  <a class='pl' href='?page=1" + clinktail + "'>第1页</a> | ";
        }
        else
        {
          pageview += "  <font color='#666666'> 第1页 </font> | ";
          pageviewtop += "  <font color='#666666'> 第1页 </font> | ";
        }
        if (x > 1)
        {
          pageviewtop += " <a class='pl' href='?page=" + (x - 1) + "" + clinktail + "'>上1页</a> ";
        }
        else
        {
          pageviewtop += " <font color='#666666'>上1页</font> ";
        }
        if (x > ((x - 1) / 10) * 10 && x > 10)
        {
          pageview += "<a class='pl' href='?page=" + ((x - 1) / 10) * 10 + "" + clinktail + "' onclink='return false;'>上10页</a>";
        }
        //if (((x-1) / 10) * 10 + 10) >= n )
        if (((x - 1) / 10) * 10 + 10 >= n)
        {
          endpage = n;
        }
        else
        {
          endpage = ((x - 1) / 10) * 10 + 10;
        }
        for (i = ((x - 1) / 10) * 10 + 1; i <= endpage; ++i)
        {
          if (i == x)
          {
            pageview += " <font color='#ff0000'><b>" + i + "</b></font>";
          }
          else
          {
            pageview += " <a class='pl' href='?page=" + i + "" + clinktail + "'>" + i + "</a>";
          }
        }
        if (x < n)
        {
          pageviewtop += " <a class='pl' href='?page=" + (x + 1) + "" + clinktail + "'>下1页</a> ";
        }
        else
        {
          pageviewtop += " <font color='#666666'>下1页</font> ";
        }
        if (endpage != n)
        {
          pageview += " <a class='pl' href='?page=" + (endpage + 1) + "" + clinktail + "' class='pl' onclink='return false;'>下10页</a> | ";
        }
        else
        {
          pageview += " | ";
        }
        if (x < n)
        {
          pageview += " <a class='pl' href='?page=" + n + "" + clinktail + "' class='pl'>第" + n + "页</a> ";
          pageviewtop += " | <a class='pl' href='?page=" + n + "" + clinktail + "' class='pl'>第" + n + "页</a> ";
        }
        else
        {
          pageview += "<font color='#666666'> 第" + n + "页 </font>";
          pageviewtop += " | <font color='#666666'> 第" + n + "页 </font>";
        }
        page_view.text = pageview.tostring();
      }
      else
      {
        page_view.text = "";
      }
    }
    //带第一页和最后一页
    public static string paging2(string para, int sumpage, int page, int count)
    {
      string result = string.empty;
      if (page > sumpage)
      {
        page = 1;
      }
      stringbuilder sb = new stringbuilder();
      if (sumpage > 0)
      {
        if (sumpage != 1)
        {
          //第一页
          sb.append(string.format("<a href=\"?page={0}{1}\"><img src=\"images/first-icon.gif\" border=\"0\"/></a>  ", new object[] { 1, para }));
          switch (page)
          {
            case 1:
              //前一页图片
              sb.append(string.format("<a>{0}</a>", new object[] { "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
              sb.append(string.format("<a>上一页</a><a href=\"?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "下一页" }));
              //后一页图片
              sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
              break;
            default:
              if (sumpage == page)
              {
                //前一页图片
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a><a>下一页</a> ", new object[] { page - 1, para, "上一页" }));
                //后一页图片
                sb.append(string.format("<a>{0}</a>", new object[] { "<img src=\"images/right-icon.gif\" />" }));
              }
              else
              {
                //前一页图片
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a> <a href=\"?page={3}{4}\">{5}</a> ",
                  new object[] { page - 1, para, "上一页", page + 1, para, "下一页" }));
                //后一页图片
                sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
              }
              break;
          }
          //最后一页图片
          sb.append(string.format("  <a href=\"?page={0}{1}\"><img src=\"images/last-icon.gif\" border=\"0\"/></a>  ", new object[] { sumpage, para }));
        }
        sb.append(string.format("第{0}页/共{1}页 共{2}条", new object[] { page, sumpage, count }));
      }
      return sb.tostring();
    }
    public static string paging3(string url, string para, int sumpage, int page, int count)
    {
      string result = string.empty;
      if (page > sumpage)
      {
        page = 1;
      }
      stringbuilder sb = new stringbuilder();
      if (sumpage > 0)
      {
        if (sumpage != 1)
        {
          //第一页
          sb.append(string.format("<a href=\"{2}?page={0}{1}\">首页</a>", new object[] { 1, para, url }));
          switch (page)
          {
            case 1:
              //前一页图片
              // sb.append(string.format("<a>{0}</a>", new object[] { "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
              sb.append(string.format("<a>上一页</a><a href=\"{3}?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "下一页", url }));
              //后一页图片
              // sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
              break;
            default:
              if (sumpage == page)
              {
                //前一页图片
                //sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
                sb.append(string.format("<a href=\"{3}?page={0}{1}\">{2}</a><a>下一页</a> ", new object[] { page - 1, para, "上一页", url }));
                //后一页图片
                //sb.append(string.format("<a>{0}</a>", new object[] { "<img src=\"images/right-icon.gif\" />" }));
              }
              else
              {
                //前一页图片
                //sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/left-icon.gif\" border=\"0\"/>" }));
                sb.append(string.format("<a href=\"{6}?page={0}{1}\">{2}</a> <a href=\"{6}?page={3}{4}\">{5}</a> ",
                  new object[] { page - 1, para, "上一页", page + 1, para, "下一页", url }));
                //后一页图片
                //sb.append(string.format("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/right-icon.gif\" border=\"0\"/>" }));
              }
              break;
          }
          //最后一页图片
          sb.append(string.format("<a href=\"{2}?page={0}{1}\">末页</a>  ", new object[] { sumpage, para, url }));
        }
        sb.append(string.format("第{0}页/共{1}页 共{2}条", new object[] { page, sumpage, count }));
      }
      return sb.tostring();
    }
    #endregion
    #region 日期格式判断
    /// <summary>
    /// 日期格式字符串判断
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool isdatetime(string str)
    {
      try
      {
        if (!string.isnullorempty(str))
        {
          datetime.parse(str);
          return true;
        }
        else
        {
          return false;
        }
      }
      catch
      {
        return false;
      }
    }
    #endregion
    #region 是否由特定字符组成
    public static bool iscontainsamechar(string strinput)
    {
      string charinput = string.empty;
      if (!string.isnullorempty(strinput))
      {
        charinput = strinput.substring(0, 1);
      }
      return iscontainsamechar(strinput, charinput, strinput.length);
    }
    public static bool iscontainsamechar(string strinput, string charinput, int leninput)
    {
      if (string.isnullorempty(charinput))
      {
        return false;
      }
      else
      {
        regex regnumber = new regex(string.format("^([{0}])+$", charinput));
        //regex regnumber = new regex(string.format("^([{0}]{{1}})+$", charinput,leninput));
        match m = regnumber.match(strinput);
        return m.success;
      }
    }
    #endregion
    #region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
    /// <summary>
    /// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
    /// </summary>
    public static bool iscontainspecchar(string strinput)
    {
      string[] list = new string[] { "123456", "654321" };
      bool result = new bool();
      for (int i = 0; i < list.length; i++)
      {
        if (strinput == list[i])
        {
          result = true;
          break;
        }
      }
      return result;
    }
    #endregion
  }
}

希望本文所述对大家的c#程序设计有所帮助。