1 /**//// <summary>
2 /// 检验日期格式是否正确
3 /// </summary>
4 public string IsDateFormat(string strDate)#region public string IsDateFormat(string strDate)
5 public string IsDateFormat(string strDate)
6 {
7 strDate = strDate.Trim();
8
9 Regex r1 = new Regex(@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$");
10 Regex r2 = new Regex(@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$");
11 Regex r3 = new Regex(@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$");
12 Regex r4 = new Regex(@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{0,3})$");
13
14 // 取得日期的年,月,日
15 string year, month, date;
16
17 if(Regex.IsMatch(strDate,@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{3})$"))
18 {
19 year = r4.Match(strDate).Result("${year}");
20 month = r4.Match(strDate).Result("${month}");
21 date = r4.Match(strDate).Result("${day}");
22 }
23 else if (Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$"))
24 {
25 year = r1.Match(strDate).Result("${year}");
26 month = r1.Match(strDate).Result("${month}");
27 date = r1.Match(strDate).Result("${day}");
28 }
29 else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$"))
30 {
31 year = r2.Match(strDate).Result("${year}");
32 month = r2.Match(strDate).Result("${month}");
33 date = r2.Match(strDate).Result("${day}");
34 }
35 else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$"))
36 {
37 year = r3.Match(strDate).Result("${year}");
38 month = r3.Match(strDate).Result("${month}");
39 date = r3.Match(strDate).Result("${day}");
40 }
41 else
42 {
43 return "error";
44 }
45
46 // 最后检查日期的正确性
47 try
48 {
49 System.DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(date));
50 return dt.ToString("yyyy-MM-dd");
51 }
52 catch
53 {
54 return "error";
55 }
56 }
57 #endregion
58
59 ------------------------------------------------------------------------------------------
60
61 /**//// <summary>
62 /// 检验Email字符串格式是否正确
63 /// </summary>
64 public bool IsEmailFormat(string strEmail)#region public bool IsEmailFormat(string strEmail)
65 public bool IsEmailFormat(string strEmail)
66 {
67 return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
68 }
69 #endregion
70
71 ------------------------------------------------------------------------------------------
72
73
74 /**//// <summary>
75 /// 转换十五位身份证为十八位的函数。
76 /// </summary>
77 public string ConvertIDCard15to18(string strTemp)#region public string ConvertIDCard15to18(string strTemp)
78 public string ConvertIDCard15to18(string strTemp)
79 {
80 int[] arrInt = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
81 string arrCh="10X98765432";
82 int nTemp = 0;
83 if(strTemp.Length==15)
84 {
85 strTemp = strTemp.Substring(0,6) + "19" + strTemp.Substring(6,strTemp.Length-6);
86 for(int i = 0; i < strTemp.Length; i++)
87 {
88 nTemp += int.Parse(strTemp.Substring(i, 1).ToString()) * arrInt[i];
89 }
90 strTemp += arrCh[nTemp % 11];
91 }
92 char dd=arrCh[nTemp % 11];
93 return strTemp;
94 }
95 #endregion
96
97 ------------------------------------------------------------------------------------------
98
99 下在为判断ASCII码的函数组,仅支持中英文
100
101 /**//// <summary>
102 /// 是否为双字节字符。
103 /// </summary>
104 public static bool IsTwoBytesChar(char chr)
105 {
106 string str =chr.ToString();
107 // 使用中文支持编码
108 Encoding ecode = Encoding.GetEncoding("GB18030");
109 if (ecode.GetByteCount(str) == 2)
110 {
111 return true;
112 }
113 else
114 {
115 return false;
116 }
117 }
118
119 /**//// <summary>
120 /// 得到字符的ASCII码
121 /// </summary>
122 public static int ASCII(char chr)
123 {
124 Encoding ecode = Encoding.GetEncoding("GB18030");
125 Byte[] codeBytes = ecode.GetBytes(chr.ToString());
126 if ( IsTwoBytesChar(chr) )
127 {
128 // 双字节码为高位乘256,再加低位
129 // 该为无符号码,再减65536
130 return (int)codeBytes[0] * 256 + (int)codeBytes[1] - 65536;
131 }
132 else
133 {
134 return (int)codeBytes[0];
135 }
136 }
2 /// 检验日期格式是否正确
3 /// </summary>
4 public string IsDateFormat(string strDate)#region public string IsDateFormat(string strDate)
5 public string IsDateFormat(string strDate)
6 {
7 strDate = strDate.Trim();
8
9 Regex r1 = new Regex(@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$");
10 Regex r2 = new Regex(@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$");
11 Regex r3 = new Regex(@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$");
12 Regex r4 = new Regex(@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{0,3})$");
13
14 // 取得日期的年,月,日
15 string year, month, date;
16
17 if(Regex.IsMatch(strDate,@"^(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})/(?<year>[1-9][0-9]{3})$"))
18 {
19 year = r4.Match(strDate).Result("${year}");
20 month = r4.Match(strDate).Result("${month}");
21 date = r4.Match(strDate).Result("${day}");
22 }
23 else if (Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})/(?<month>[0-9]{1,2})/(?<day>[0-9]{1,2})$"))
24 {
25 year = r1.Match(strDate).Result("${year}");
26 month = r1.Match(strDate).Result("${month}");
27 date = r1.Match(strDate).Result("${day}");
28 }
29 else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})-(?<month>[0-9]{1,2})-(?<day>[0-9]{1,2})$"))
30 {
31 year = r2.Match(strDate).Result("${year}");
32 month = r2.Match(strDate).Result("${month}");
33 date = r2.Match(strDate).Result("${day}");
34 }
35 else if(Regex.IsMatch(strDate,@"^(?<year>[1-9][0-9]{0,3})年(?<month>[0-9]{1,2})月(?<day>[0-9]{1,2})日$"))
36 {
37 year = r3.Match(strDate).Result("${year}");
38 month = r3.Match(strDate).Result("${month}");
39 date = r3.Match(strDate).Result("${day}");
40 }
41 else
42 {
43 return "error";
44 }
45
46 // 最后检查日期的正确性
47 try
48 {
49 System.DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(date));
50 return dt.ToString("yyyy-MM-dd");
51 }
52 catch
53 {
54 return "error";
55 }
56 }
57 #endregion
58
59 ------------------------------------------------------------------------------------------
60
61 /**//// <summary>
62 /// 检验Email字符串格式是否正确
63 /// </summary>
64 public bool IsEmailFormat(string strEmail)#region public bool IsEmailFormat(string strEmail)
65 public bool IsEmailFormat(string strEmail)
66 {
67 return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
68 }
69 #endregion
70
71 ------------------------------------------------------------------------------------------
72
73
74 /**//// <summary>
75 /// 转换十五位身份证为十八位的函数。
76 /// </summary>
77 public string ConvertIDCard15to18(string strTemp)#region public string ConvertIDCard15to18(string strTemp)
78 public string ConvertIDCard15to18(string strTemp)
79 {
80 int[] arrInt = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
81 string arrCh="10X98765432";
82 int nTemp = 0;
83 if(strTemp.Length==15)
84 {
85 strTemp = strTemp.Substring(0,6) + "19" + strTemp.Substring(6,strTemp.Length-6);
86 for(int i = 0; i < strTemp.Length; i++)
87 {
88 nTemp += int.Parse(strTemp.Substring(i, 1).ToString()) * arrInt[i];
89 }
90 strTemp += arrCh[nTemp % 11];
91 }
92 char dd=arrCh[nTemp % 11];
93 return strTemp;
94 }
95 #endregion
96
97 ------------------------------------------------------------------------------------------
98
99 下在为判断ASCII码的函数组,仅支持中英文
100
101 /**//// <summary>
102 /// 是否为双字节字符。
103 /// </summary>
104 public static bool IsTwoBytesChar(char chr)
105 {
106 string str =chr.ToString();
107 // 使用中文支持编码
108 Encoding ecode = Encoding.GetEncoding("GB18030");
109 if (ecode.GetByteCount(str) == 2)
110 {
111 return true;
112 }
113 else
114 {
115 return false;
116 }
117 }
118
119 /**//// <summary>
120 /// 得到字符的ASCII码
121 /// </summary>
122 public static int ASCII(char chr)
123 {
124 Encoding ecode = Encoding.GetEncoding("GB18030");
125 Byte[] codeBytes = ecode.GetBytes(chr.ToString());
126 if ( IsTwoBytesChar(chr) )
127 {
128 // 双字节码为高位乘256,再加低位
129 // 该为无符号码,再减65536
130 return (int)codeBytes[0] * 256 + (int)codeBytes[1] - 65536;
131 }
132 else
133 {
134 return (int)codeBytes[0];
135 }
136 }