DataMatch
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// Copyright (C)/// 文件名:DataMatch.cs
/// 版本:version 1.00.00
/// 创建标识:2006-05-01
/// 功能说明:验证数据是否符合统一规则的规定
/// 注意事项:
/// 匹配数据
///
/// 更新记录:
/// </summary>
using System;
using System.Text.RegularExpressions;
namespace KTVSolution.Common
{
/// <summary>
/// 验证数据是否符合统一规则的规定
/// </summary>
public class DataMatch
{
public DataMatch()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 验证用户账号是否符合规则
/// </summary>
/// <param name="LogonName">企业用户名</param>
/// <returns>true:符合;false:不符合</returns>
public static bool MatchLogonName(string LogonName)
{
//长度在3-20个字符之间任意字符,除了"'"号。
if (LogonName.IndexOf("'")>=0)
{
return false;
}
byte[] bt = System.Text.Encoding.Default.GetBytes(LogonName);
if (bt.Length>20 || bt.Length<3)
{
return false;
}
else
{
return true;
}
}//end MatchLogonName
/// <summary>
/// 验证密码是否符合简单密码规则
/// 2005-08-09
/// </summary>
/// <param name="Password">密码</param>
/// <returns>true:符合;false:不符合</returns>
public static bool MatchPassword(string Password)
{
Regex r = new Regex(@"^([\S]{6,20})$");
return r.Match(Password).Success;
}//end MatchPasswordSimple
/// <summary>
/// 验证字符串是否为标准的日期时间格式
/// 2005-08-10 /// </summary>
/// <param name="DateString">日期时间字符串。形如1900-01-01 00:00:00</param>
/// <returns>true:符合;false:不符合</returns>
public static bool MatchDateTime(string DateTimeString)
{
try
{
DateTime.Parse(DateTimeString);
Regex r = new Regex(@"^(\d{4,4}-\d{2,2}-\d{2,2}) (\d{2,2}:\d{2,2}:\d{2,2})$");
return r.Match(DateTimeString).Success;
}//end try
catch
{
return false;
}
}//end MatchDateTime
/// <summary>
/// 验证字符串是否为合法的日期格式
/// 2005-08-10 /// </summary>
/// <param name="DateString">日期字符串。形如1900-01-01</param>
/// <returns>true:符合;false:不符合</returns>
public static bool MatchDate(string DateString)
{
try
{
DateTime.Parse(DateString);
Regex r = new Regex(@"^(\d{4,4}-\d{2,2}-\d{2,2})$");
return r.Match(DateString).Success;
}//end try
catch
{
return false;
}
}//end MatchDate
/// <summary>
/// 判断是否是全数字
/// 2005-08-09 /// </summary>
/// <param name="input">需要匹配的字符串</param>
/// <returns>true:是;false:不是</returns>
public static bool AllIsNumeral(string input)
{
Regex r = new Regex(@"^(\d{1,})$");
return r.Match(input).Success;
}//end AllIsNumber
/// <summary>
/// 判断是否是全字母
/// 2005-08-09
/// </summary>
/// <param name="input">需要匹配的字符串</param>
/// <returns>true:是;false:不是</returns>
public static bool AllIsLetter(string input)
{
Regex r = new Regex(@"^([a-zA-Z]{1,})$");
return r.Match(input).Success;
}//end AllIsLetter
/// <summary>
/// 检查用户输入字段是否超过长度
/// 2005-8-15 /// </summary>
/// <param name="fvalue">输入字段</param>
/// <param name="length">长度</param>
/// <returns>true为正常,false为超过</returns>
public static bool MatchInputLength(string fvalue,int length)
{
byte[] bt = System.Text.Encoding.Default.GetBytes(fvalue);
if(bt.Length>length)
{
//超过
return false;
}
else
{
//正常
return true;
}
}
/// <summary>
/// 验证电话是否正确
/// 2005-8-26 /// </summary>
/// <param name="userCount"></param>
/// <returns></returns>
public static bool MatchTelephone(string telephone)
{
//匹配电话
Regex reg = new Regex(@"(((\(\d{3,4}\))|(\d{3,4}))(-|)\d{7,8})|\d{11}");
return reg.Match(telephone).Success;
}
/// <summary>
/// 验证传真号码是否正确
/// 2005-8-26
/// </summary>
/// <param name="userCount"></param>
/// <returns></returns>
public static bool MatchFax(string fax)
{
//匹配传真号码
Regex reg = new Regex(@"(((\(\d{3,4}\))|(\d{3,4}))(-|)\d{7,8})");
return reg.Match(fax).Success;
}
/// <summary>
/// 验证Email帐号是否正确
/// 2005-8-26 /// </summary>
/// <param name="userCount"></param>
/// <returns></returns>
public static bool MatchEmail(string email)
{
//匹配Email
Regex reg = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
return reg.Match(email).Success;
}
/// <summary>
/// 匹配正确的URL地址
/// 2005-8-26
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool MatchURL(string url)
{
//匹配URL
Regex reg = new Regex(@"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?");
return reg.Match(url).Success;
}
/// <summary>
/// 匹配正确的IP地址
/// 2005-8-26 /// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool MatchIP(string ip)
{
//匹配IP
Regex reg = new Regex(@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$");
return reg.Match(ip).Success;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
#region 全角转半角
/// <summary>
/// 全角转半角
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string qjtobj(string str)
{
string QJstr = str;
char[] c = QJstr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
{
if (b[1] == 255)
{
b[0] = (byte)(b[0] + 32);
b[1] = 0;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
string strNew = new string(c);
//MessageBox.Show(strNew);
return strNew;
}
#endregion