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
C# 验证类(使用正则表达式 验证文本框)的更多相关文章
- jquery正则表达式显示文本框输入范围 只能输入数字、小数、汉字、英文字母的方法
正则表达式限制文本框只能输入数字 许多时候我们在制作表单时需要限制文本框输入内容的类型,下面我们用正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等各类代码.1.文本框只能输入数字代码(小数点 ...
- js正则表达式限制文本框只能输入数字,小数点,英文字母
1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafter ...
- JS 正则表达式 控制文本框只能输入中文、英文、数字与指定特殊符号
JS 正则表达式 控制文本框只能输入中文.英文.数字与指定特殊符号(屏蔽表情输入) onkeyup:释放键盘事件 onpaste:粘贴事件 oncontextmenu :鼠标右击事件 只能输入中文: ...
- JQuery基础原理 与实例 验证表单 省市联动 文本框判空 单选 复选 判空 下拉判空 确认密码判等
JQuery 基础原理 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- js使用正则表达式实现文本框只能输入数字和小数点
第一种情况:且限制小数点前最大3位数,小数点后最大3为三位 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- ASP.NET c# textbox 正则表达式 文本框只允许输入数字(验证控件RegularExpressionValidator )
<input type="text" name="test" onKeyUp="test1.value=(this.value=this.val ...
- JS添加删除一组文本框并对输入信息加以验证
在做项目中遇到这样一个问题,就是我们需要添加几组数据到数据库,但是具体几组数据不确定,有客户来填写,比如我们需要添加打折策略,可能个策略有很多组方案,比如“满100打5折,满200打4折,满500打3 ...
- HTML 限制文本框只能输入特定字符(比如数字 onkeyup+onafterpaste)
正则表达式1 <td><asp:TextBox ID="TextBox_username" Width="250" runat="s ...
- 文本框只允许输入数字.net/javascript
<input type="text" name="test" onKeyUp="test1.value=(this.value=this.val ...
- EasyUI表单验证,自定义插件验证,自定义js插件验证,远程验证,常见手机号,中英文,qq等验证规则验证
{ field : 'startPort', title : "起始端口", editor: "text", width : 50, editor: { ...
随机推荐
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Hihocoder 太阁最新面经算法竞赛18
Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...
- C/C++ 标准输入输出重定向
转载自:http://www.cnblogs.com/hjslovewcl/archive/2011/01/10/2314356.html 这个对经常在OJ上做题的童鞋们很有用.OJ基本都是用标准输入 ...
- jquery基本操作笔记
来源于:http://www.cnblogs.com/webcome/p/5484005.html jq和js 可以共存,不能混用: 1 2 3 4 5 6 $('.box').css('backgr ...
- 基于bootstrap样式的tree,
<!doctype html><html lang="zh"><head> <meta charset="UTF-8" ...
- HDF5基本使用方法
HDF5, 大量(海量?)数据存储的一种解决方案. HDF的全称是Hiearchical Data Format, 5是版本号(未考证过TODO). 一个HDF5文件操作起来就像一个独立的文件系统. ...
- csv to splite db form
termsql: https://github.com/tobimensch/termsql termsql -i textfile -d ',' -o sqlite.db 添加列名 termsql ...
- C#面向对象设计模式纵横谈——5.Factory Method 工厂方法模式(创建型模式)
动机 (Motivation) 在软件系统中,经常面临着“某个对象”的创建工作; 由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何应对这种变化?如何提供一种“封装机制” ...
- 【poj2151】 Check the difficulty of problems
http://poj.org/problem?id=2151 (题目链接) 题意 T支队伍,一共M道题,第i支队伍解出第j道题的概率为p[i][j].问每支队伍至少解出1道题并且解题最多的的队伍至少解 ...
- 关于 feature team 的一些内容
矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...