日期:2014-05-18  浏览次数:20981 次

C#结合正则表达式判断各种用户输入合法性
using System;
using System.Text.RegularExpressions;

 namespace SG_VQCDataCollection
{
    /// <summary>
    /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查
     /// </summary>
     public class MetarnetRegex
    {

         private static MetarnetRegex instance = null;
         public static MetarnetRegex GetInstance()
         {
           if (MetarnetRegex.instance == null)
             {
                 MetarnetRegex.instance = new MetarnetRegex();
             }
             return MetarnetRegex.instance;
         }
         private MetarnetRegex()
         {
         }

         /// <summary>
         /// 判断输入的字符串只包含汉字
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsChineseCh(string input)
         {
             //Regex regex = new Regex("^[\一-\龥]+$");

            //改了一下

             Regex regex = new Regex("^[\一-\龥]+$");
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
         /// 也可以不用,区号与本地号间可以用连字号或空格间隔,
         /// 也可以没有间隔
         /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsPhone(string input)
         {
             string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
             Regex regex = new Regex(pattern);
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 判断输入的字符串是否是一个合法的手机号
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsMobilePhone(string input)
         {
             Regex regex = new Regex("^13\\d{9}$");
             return regex.IsMatch(input);

         }


         /// <summary>
         /// 判断输入的字符串只包含数字
         /// 可以匹配整数和浮点数
         /// ^-?\d+$|^(-?\d+)(\.\d+)?$
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsNumber(string input)
         {
             string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
             Regex regex = new Regex(pattern);
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 匹配非负整数
         ///
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsNotNagtive(string input)
         {
             Regex regex = new Regex(@"^\d+$");
             return regex.IsMatch(input);
         }
         /// <summary>
         /// 匹配正整数
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsUint(string input)
         {
             Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
             return regex.IsMatch(input);
         }
         /// <summary>
         /// 判断输入的字符串字包含英文字母
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsEnglisCh(string input)
         {
             Regex regex = new Regex("^[A-Za-z]+$");
             return regex.IsMatch(input);
         }


         /// <summary>
         /// 判断输入的字符串是否是一个合法的Email地址
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static