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 bool IsEmail(string input)
         {
             string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
             Regex regex = new Regex(pattern);
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 判断输入的字符串是否只包含数字和英文字母
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsNumAndEnCh(string input)
         {
             string pattern = @"^[A-Za-z0-9]+$";
             Regex regex = new Regex(pattern);
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 判断输入的字符串是否是一个超链接
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsURL(string input)
         {
             //string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
             string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
             Regex regex = new Regex(pattern);
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 判断输入的字符串是否是表示一个IP地址
         /// </summary>
         /// <param name="input">被比较的字符串</param>
         /// <returns>是IP地址则为True</returns>
         public static bool IsIPv4(string input)
         {

             string[] IPs = input.Split(‘.‘);
             Regex regex = new Regex(@"^\d+$");
             for (int i = 0; i < IPs.Length; i++)
             {
                 if (!regex.IsMatch(IPs[i]))
                 {
                     return false;
                 }
                 if (Convert.ToUInt16(IPs[i]) > 255)
                {
                     return false;
                 }
             }
             return true;
         }

         /// <summary>
         /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
         /// </summary>
         /// <param name="input">需要计算的字符串</param>
         /// <returns>返回字符串的长度</returns>
         public static int GetCount(string input)
         {
             return Regex.Replace(input, @"[\一-\龥/g]", "aa").Length;
         }

         /// <summary>
         /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
         /// </summary>
         /// <param name="pattern">要匹配的正则表达式模式。</param>
         /// <param name="input">要搜索匹配项的字符串</param>
         /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns>
         public static bool IsMatch(string pattern, string input)
         {
             Regex regex = new Regex(pattern);
             return regex.IsMatch(input);
         }

         /// <summary>
         /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
         /// </summary>
         /// <param name="pattern">模式字符串</param>
         /// <param name="input">输入字符串</param>
         /// <param name="replacement">用于替换的字符串</param>
         /// <returns>返回被替换后的结果</returns>
         public static string Replace(string pattern, string input, string replacement)
         {
             Regex regex = new Regex(pattern);
             return regex.Replace(input, replacement);
         }

         /// <summary>
         /// 在由正则表达式模式定义的位置拆分输入字符串。
         /// </summary>
         /// <param name="pattern">模式字符串</param>
         /// <param name="input">输入字符串</param>
         /// <returns></returns>
         public static string[] Split(string pattern, string input)
         {
             Regex regex = new Regex(pattern);
             return regex.Split(input);
         }

         /// <summary>
         /// 判断输入的字符串是否是合法的IPV6 地址
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
         public static bool IsIPV6(string input)
         {
             string pattern = "";
             string temp = input;
             string[] strs = temp.Split(‘:‘);
             if (strs.Length > 8)
             {
                 return false;
             }
             int count = MetarnetRegex.GetStringCount(input, "::");
             if (count > 1)
             {
                 return false;
             }
             else if (count == 0)
             {
                 pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";

                 Regex regex = new Regex(pattern);
                 return regex.IsMatch(input);
             }
             else
             {
                 pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
                 Regex regex1 = new Regex(pattern);
                 return regex1.IsMatch(input);
             }

         }
         /* *******************************************************************
         * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8
         * 2、判断输入的IPV6字符串中是否有“::”。
         * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断
         * 4、如果有“::” ,判断"::"是否止出现一次
         * 5、如果出现一次以上 返回false
         * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
         * ******************************************************************/
         /// <summary>
         /// 判断字符串compare 在 input字符串中出现的次数
         /// </summary>
         /// <param name="input">源字符串</param>
         /// <param name="compare">用于比较的字符串</param>
         /// <returns>字符串compare 在 input字符串中出现的次数</returns>
         private static int GetStringCount(string input, string compare)
         {
             int index = input.IndexOf(compare);
             if (index != -1)
             {
                return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
             }
             else
             {
                 return 0;
             }

         }
     }
 }

转载自:http://blog.csdn.net/hwt0101/article/details/7788480

时间: 2025-01-01 23:05:40

C# 验证类(使用正则表达式 验证文本框)的相关文章

jquery正则表达式显示文本框输入范围 只能输入数字、小数、汉字、英文字母的方法

正则表达式限制文本框只能输入数字 许多时候我们在制作表单时需要限制文本框输入内容的类型,下面我们用正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等各类代码.1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">2.只能输入数字,能输

js使用正则表达式对文本框进行限制输入

1.文本框只能输入大于等于0,小于等于100,保留两位小数的数字,小数点只能出现一次 function verifyRatio(obj) { // 值允许输入0-100的小数 obj.value = obj.value.replace(/[^\d.]/g, ""); //先把非数字的都替换掉,除了数字和. obj.value = obj.value.replace(/^\./g, ""); //必须保证第一个为数字而不是. obj.value = obj.value

正则表达式控制文本框只允许填数字、负号、点号

有时候,我们需要控制文本框,只允许用户输入可以为负数的浮点值,例如:-23.563:而不允许出现23.563qq这样的值. 这里我们还是看代码吧,用正则表达式很容易就能实现以上需求: 前台页面: 1 <asp:TextBox ID="txtValueEnter" runat="server" Width="228px"></asp:TextBox> 后台页面: 1 //注意负号.点号都需要使用双个斜杠转换,单引号单个斜杠转

JS添加删除一组文本框并对输入信息加以验证

在做项目中遇到这样一个问题,就是我们需要添加几组数据到数据库,但是具体几组数据不确定,有客户来填写,比如我们需要添加打折策略,可能个策略有很多组方案,比如“满100打5折,满200打4折,满500打3折”等等,这是作为一组方案来执行的,但是并不确定一组方案中有几个子方案,所以,这里我用JS进行添加删除子方案,并要对方案输入的正确性加以判断,并且通过json传输写入数据库,这里我们主要写如果添加删除一组子项目和如果给每个文本框添加验证. 动态添加一组文本框: var countTable = 0;

页面文本框输入限制[正则表达式]

文本框只允许输入数字:<input type="text" name="test" onKeyUp="test1.value=(this.value=this.value.replace(/\D/g,'').substring(0,6)).substring(0,3)" > <br /><input name="test1" type="text" > 用Up和Down有

文本框只允许输入数字.net/javascript

<input type="text" name="test" onKeyUp="test1.value=(this.value=this.value.replace(/\D/g,'').substring(0,6)).substring(0,3)" > <br /><input name="test1" type="text" > 用Up和Down有区别 只能输入数字.字

【Java GUI】文本框和文本区

文本框 文本框(JTextField)是界面中用于输入和输出一行文本的框.JTextField类用来建立文本框.与文本框相关的接口是ActionListener. 文本框处理程序的基本内容有以下几个方面: ①声明一个文本框名. ②建立一个文本框对象. ③将文本框对象加入到某个容器. ④对需要控制的文本框对象注册监视器,监听文本框的输入结束(即输入回车键)事件. ⑤一个处理文本框事件的方法,完成对截获事件进行判断和处理. JTextField类的主要构造方法 JTextField();//文本框的

JavaScript 中怎样判断文本框只能输出英文字母、汉字和数字,不能输入特殊字符!

JS-只能输入中文和英文2008-11-08 10:17在js中用正则表达式对象(RegExp)判断中文 ^[\u0391-\uFFE5]+$英文 ^[A-Za-z]+$中文和英文/^[\u0391-\uFFE5A-Za-z]+$/ js正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码 1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpa

正则表达式验证input文本框

方便以后的查找,直接copy代码在这里了. eg: //公司邮箱验证 if ($("#Email").val() != "") { var myreg = /^([a-zA-Z0-9_\.\-])+\@@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (!myreg.test($("#Email").val())) { alert("温馨提示:\n\n请输入有效的公司邮箱,谢谢!"

如何验证文本框中的内容是否为数字

如何验证文本框中的内容是否为数字:在某些情况下可能需要让文本框中的内容只能够输入数字,例如手机号码或者邮编之类的,下面简单介绍一下如何实现此功能.下面是验证数字的正则表达式: "^\\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\\d+)|(0+))$" //非正整数(负整数 + 0) "^-[0-9]*[1-9][0-9]*$" //负整数 "^-?\