C#中将用户输入的座机区号查询号先按字符"-"拆分然后再判断是否满足前面的区号

3698人阅读
using System.Text.RegularE
namespace SG_VQCDataCollection
/// &summary&
/// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查
/// &/summary&
public class MetarnetRegex
private static MetarnetRegex instance =
public static MetarnetRegex GetInstance()
if (MetarnetRegex.instance == null)
MetarnetRegex.instance = new MetarnetRegex();
return MetarnetRegex.
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.L i++)
if (!regex.IsMatch(IPs[i]))
if (Convert.ToUInt16(IPs[i]) & 255)
/// &summary&
/// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
/// &/summary&
/// &param name=&input&&需要计算的字符串&/param&
/// &returns&返回字符串的长度&/returns&
public static int GetCount(string input)
return Regex.Replace(input, @&[\一-\龥/g]&, &aa&).L
/// &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 =
string[] strs = temp.Split(':');
if (strs.Length & 8)
int count = MetarnetRegex.GetStringCount(input, &::&);
if (count & 1)
else if (count == 0)
pattern = @&^([\da-f]{1,4}:){7}[\da-f]{1,4}$&;
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
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);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:167729次
积分:2162
积分:2162
排名:第13680名
原创:40篇
转载:44篇
评论:13条
(1)(2)(2)(1)(5)(1)(1)(5)(1)(1)(3)(6)(1)(1)(1)(1)(1)(2)(1)(5)(6)(7)(1)(1)(4)(2)(2)(2)(1)(1)(1)(2)(2)(1)(1)(8)c#判断输入文字是否是数字 - 开源中国社区
当前访客身份:游客 [
当前位置:
c#判断输入文字是否是数字
方案一:/**//// &summary& /// 名称:IsNumberic /// 功能:判断输入的是否是数字 /// 参数:string oText:源文本 /// 返回值: bool true:是 false:否 /// &/summary& public bool IsNumberic(string oText) { try { int var1=Convert.ToInt32 (oText);
&&&& try catch方法&&&& 例:&&&& try&&&& {&&&&&& Convert.ToInt32(&123&):&&&&&& Console.Write(&是数字&);&&&& }&&&& catch(Exception ex)&&&& {&&&&&& Console.Write(&非数字&);&&&& }&&&& 注:如果有很多字符串要求判断,此方法需要大量的try catch 以及finally来处理后续的程序.不建议使用此方法。
改进一下:因为可以转int 可以转Decimal&&& public bool IsNumberic(string oText)&&& {&&&&&&& try&&&&&&& {&&&&&&&&&&& Decimal Number = Convert.ToDecimal (oText);&&&&&&&&&&&&&&&&&& }&&&&&&& catch&&&&&&& {&&&&&&&&&&&&&&&&&& }&&& }
方案二://如果是纯数字还可以采用ASCII码进行判断/// &summary&&& /// 判断是否是数字&& /// &/summary&&& /// &param name=&str&&字符串&/param&&& /// &returns&bool&/returns&&& public bool IsNumeric(string str)&& {&& &&& if (str == null || str.Length == 0)&& &&&&&&&&& &&& System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();&& &&& byte[] bytestr = ascii.GetBytes(str);&& &&& foreach (byte c in bytestr)&& &&& {&& &&&&&&& if (c & 48 || c & 57)&& &&&&&&& {&& &&&&&&&&&&&&& &&&&&&& }&& &&& }&& &&&&& }&
方案三:&&&& 正则表达式方法&&&& 例:&&&& //引用正则表达式类&&&& using&& System.Text.RegularE& &&&& Regex&& reg=new&& Regex(&^[0-9]+$&);& &&&& Match&& ma=reg.Match(text);& &&&& if(ma.Success)& &&&& {& &&&&& //是数字& &&&& }& &&&& else& &&&& {& &&&& //不是数字& &&&& }&&&& 注:此方法快捷,但不太容易掌握,尤其是正则表达式公式,如果有兴趣的朋友可以好好研究,这东西很好用的,建议使用。
方案四:&&&& Double.TryParse方法&&&& 例:&&&& bool isNum=System.Double.TryParse(&所要判断的字符串&& ,System.Globalization.NumberStyles.Integer,null,out );&&&& 注:此方法快捷,方便,很容易被掌握,但是参数很多,有兴趣的朋友可以研究一下,建议使用。&&& 参数不好用&&& 没有使用过
public static bool IsNumberic(string strnum)
{&&&&& int i = 0;&&& & bool result = int.TryParse(strnum, out i);&&&& &
方法五:新建一个类using Susing System.Collections.Gusing System.Text.RegularEnamespace LBC.Number{&&& /// &summary&&&& /// 数字判断的类&&& /// &/summary&&&& public class NumberClass&&& {&&&&&&& /// &summary&&&&&&&& /// 判断是否是数字&&&&&&& /// &/summary&&&&&&&& /// &param name=&strNumber&&要判断的字符串&/param&&&&&&&& /// &returns&&/returns&&&&&&&& public static bool IsNumber(String strNumber)&&&&&&& {&&&&&&&&&&& Regex objNotNumberPattern = new Regex(&[^0-9.-]&);&&&&&&&&&&& Regex objTwoDotPattern = new Regex(&[0-9]*[.][0-9]*[.][0-9]*&);&&&&&&&&&&& Regex objTwoMinusPattern = new Regex(&[0-9]*[-][0-9]*[-][0-9]*&);&&&&&&&&&&& String strValidRealPattern = &^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+___FCKpd___0;&&&&&&&&&&& String strValidIntegerPattern = &^([-]|[0-9])[0-9]*___FCKpd___0;&&&&&&&&&&& Regex objNumberPattern = new Regex(&(& + strValidRealPattern + &)|(& + strValidIntegerPattern + &)&);&&&&&&&&&&& return !objNotNumberPattern.IsMatch(strNumber) &&&&&&&&&&&&& !objTwoDotPattern.IsMatch(strNumber) &&&&&&&&&&&&& !objTwoMinusPattern.IsMatch(strNumber) &&&&&&&&&&&&& objNumberPattern.IsMatch(strNumber);&&&&&&& }&&&&&&& /// &summary&&&&&&&& /// 判断是否是int类型&&&&&&& /// &/summary&&&&&&&& /// &param name=&Value&&要判断的字符串&/param&&&&&&&& /// &returns&&/returns&&&&&&&& public static bool IsInt(string Value)&&&&&&& {&&&&&&&&&&& return Regex.IsMatch(Value, @&^[+-]?/d*___FCKpd___0);&&&&&&& }&&&&&&& /// &summary&&&&&&&& /// 判断是否是数字&&&&&&& /// &/summary&&&&&&&& /// &param name=&Value&&要判断的字符串&/param&&&&&&&& /// &returns&&/returns&&&&&&&& public static bool IsNumeric(string Value)&&&&&&& {&&&&&&&&&&& return Regex.IsMatch(Value, @&^[+-]?/d*[.]?/d*___FCKpd___0);&&&&&&& }&&& }}&
声明:本贴转自
原文链接:
共有0个评论
更多开发者职位上
有什么技术问题吗?
长平狐的其它问题
类似的话题C#验证用户输入信息是否包含危险字符串的方法
作者:feige
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了C#验证用户输入信息是否包含危险字符串的方法,可针对and、or、exec、insert、select等SQL操作技巧进行过滤操作,非常具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了C#验证用户输入信息是否包含危险字符串的方法。分享给大家供大家参考。具体分析如下:
这个C#函数可以用于表单输入数据的后端验证,判断用户是否提交了一些sql相关的危险注入字符
/// &summary&
/// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串
/// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。
/// &/summary&
/// &param name="input"&要检测的字符串&/param&
public static bool IsValidInput(ref string input)
if (IsNullOrEmpty(input))
//如果是空值,则跳出
//替换单引号
input = input.Replace("'", "''").Trim();
//检测攻击性危险字符串
string testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
string[] testArray = testString.Split('|');
foreach (string testStr in testArray)
if (input.ToLower().IndexOf(testStr) != -1)
//检测到攻击字符串,清空传入的值
input = "";
//未检测到攻击字符串
catch (Exception ex)
throw new Exception(ex.Message);
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 广州座机区号 的文章

 

随机推荐