using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace XClassLibrary
{
public class Validator
{
#region 匹配方法
public static bool IsMatch(string inputStr, string patternStr)
{
return IsMatch(inputStr, patternStr, false, false);
}
public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase)
{
return IsMatch(inputStr, patternStr, ifIgnoreCase, false);
}
public static bool IsMatch(string inputStr, string patternStr, bool ifValidateWhiteSpace)
{
return IsMatch(inputStr, patternStr, false, ifValidateWhiteSpace);
}
public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace)
{
if (!ifValidateWhiteSpace && string.IsNullOrWhiteSpace(inputStr))
return false;
Regex regex = null;
if (ifIgnoreCase)
regex = new Regex(patternStr, RegexOptions.IgnoreCase);
else
regex = new Regex(patternStr);
return regex.IsMatch(inputStr);
}
#endregion
#region 验证方法
public static bool IsNumber(string input)
{
double d = 0;
if (double.TryParse(input, out d))
return true;
else
return false;
}
public static bool IsInteger(string input)
{
int i = 0;
if (int.TryParse(input, out i))
return true;
else
return false;
}
public static bool IsIntegerNotNagtive(string input)
{
int i = -1;
if (int.TryParse(input, out i) && i >= 0)
return true;
else
return false;
}
public static bool IsIntegerPositive(string input)
{
int i = 0;
if (int.TryParse(input, out i) && i >= 1)
return true;
else
return false;
}
public static bool IsDecimal(string input)
{
string pattern = @"^([-+]?[1-9]\d*\.\d+|-?0\.\d*[1-9]\d*)$";
return IsMatch(input, pattern);
}
public static bool IsEnglishCharacter(string input)
{
string pattern = @"^[A-Za-z]+$";
return IsMatch(input, pattern);
}
public static bool IsIntegerAndEnglishCharacter(string input)
{
string pattern = @"^[0-9A-Za-z]+$";
return IsMatch(input, pattern);
}
public static bool IsChineseCharacter(string input)
{
string pattern = @"^[\u4e00-\u9fa5]+$";
return IsMatch(input, pattern);
}
public static bool IsIntegerLength(string input, int lengthBegin, int lengthEnd)
{
if (input.Length >= lengthBegin && input.Length <= lengthEnd)
{
int i;
if (int.TryParse(input, out i))
return true;
else
return false;
}
else
return false;
}
public static bool IsStringInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter)
{
if (!withEnglishCharacter && !withNumber && !withChineseCharacter)
return false;
StringBuilder patternString = new StringBuilder();
patternString.Append("^[");
if (withEnglishCharacter)
patternString.Append("a-zA-Z");
if (withNumber)
patternString.Append("0-9");
if (withChineseCharacter)
patternString.Append(@"\u4E00-\u9FA5");
patternString.Append("]+$");
return IsMatch(input, patternString.ToString());
}
public static bool IsStringLength(string input, int lengthBegin, int lengthEnd)
{
if (input.Length >= lengthBegin && input.Length <= lengthEnd)
return true;
else
return false;
}
public static bool IsStringLengthOnlyNumberAndEnglishCharacter(string input, int lengthBegin, int lengthEnd)
{
string pattern = @"^[0-9a-zA-z]{" + lengthBegin + "," + lengthEnd + "}$";
return IsMatch(input, pattern);
}
public static bool IsStringLengthByInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter, int lengthBegin, int lengthEnd)
{
if (!withEnglishCharacter && !withNumber && !withChineseCharacter)
return false;
StringBuilder patternString = new StringBuilder();
patternString.Append("^[");
if (withEnglishCharacter)
patternString.Append("a-zA-Z");
if (withNumber)
patternString.Append("0-9");
if (withChineseCharacter)
patternString.Append(@"\u4E00-\u9FA5");
patternString.Append("]{" + lengthBegin + "," + lengthEnd + "}$");
return IsMatch(input, patternString.ToString());
}
public static bool IsStringByteLength(string input, int lengthBegin, int lengthEnd)
{
int byteLength = Encoding.Default.GetByteCount(input);
if (byteLength >= lengthBegin && byteLength <= lengthEnd)
return true;
else
return false;
}
public static bool IsDateTime(string input)
{
DateTime dt;
if (DateTime.TryParse(input, out dt))
return true;
else
return false;
}
public static bool IsTelePhoneNumber(string input)
{
string pattern = @"^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$";
return IsMatch(input, pattern);
}
public static bool IsMobilePhoneNumber(string input)
{
string pattern = @"^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$";
return IsMatch(input, pattern);
}
public static bool IsPhoneNumber(string input)
{
string pattern = @"^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$|^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$";
return IsMatch(input, pattern);
}
public static bool IsZipCode(string input)
{
if (input.Length != 6)
return false;
int i;
if (int.TryParse(input, out i))
return true;
else
return false;
}
public static bool IsEmail(string input)
{
string pattern = @"^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$";
return IsMatch(input, pattern);
}
public static bool IsURL(string input)
{
string pattern = @"^([a-zA-Z]+://)?([\w-\.]+)(\.[a-zA-Z0-9]+)(:\d{0,5})?/?([\w-/]*)\.?([a-zA-Z]*)\??(([\w-]*=[\w%]*&?)*)$";
return IsMatch(input, pattern);
}
public static bool IsIPv4(string input)
{
string[] IPs = input.Split('.');
if (IPs.Length != 4)
return false;
int n = -1;
for (int i = 0; i < IPs.Length; i++)
{
if (i == 0 || i == 3)
{
if (int.TryParse(IPs[i], out n) && n > 0 && n < 255)
continue;
else
return false;
}
else
{
if (int.TryParse(IPs[i], out n) && n >= 0 && n <= 255)
continue;
else
return false;
}
}
return true;
}
public static bool IsIPv6(string input)
{
string pattern = @"^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$";
return IsMatch(input, pattern);
}
public static bool IsIDCard15(string input)
{
long l = 0;
if (!long.TryParse(input, out l) || l.ToString().Length != 15)
{
return false;
}
string address = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";
if (!address.Contains(input.Remove(2) + ","))
{
return false;
}
string birthdate = input.Substring(6, 6).Insert(4, "/").Insert(2, "/");
DateTime dt;
if (!DateTime.TryParse(birthdate, out dt))
{
return false;
}
return true;
}
public static bool IsIDCard18(string input)
{
long l = 0;
if (!long.TryParse(input.Remove(17), out l) || l.ToString().Length!=17 || !long.TryParse(input.Replace('x', '0').Replace('X', '0'), out l))
{
return false;
}
string address = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";
if (!address.Contains(input.Remove(2) + ","))
{
return false;
}
string birthdate = input.Substring(6, 8).Insert(6, "/").Insert(4, "/");
DateTime dt;
if (!DateTime.TryParse(birthdate, out dt))
{
return false;
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = input.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != input.Substring(17, 1).ToLower())
{
return false;
}
return true;
}
public static bool IsIDCard(string input)
{
if (input.Length == 18)
return IsIDCard18(input);
else if (input.Length == 15)
return IsIDCard15(input);
else
return false;
}
public static bool IsLongitude(string input)
{
float lon;
if (float.TryParse(input, out lon) && lon >= -180 && lon <= 180)
return true;
else
return false;
}
public static bool IsLatitude(string input)
{
float lat;
if (float.TryParse(input, out lat) && lat >= -90 && lat <= 90)
return true;
else
return false;
}
#endregion
}
}
posted @
2017-06-12 15:26
小伙子很浪
阅读(
141 )
评论()
编辑
收藏
举报