textbox输入的日期与Email地址如何验证有效?
我只知道用DateTime,但是怎么验证日期是有效的?DateTime MyDate = new DateTime(e.KeyChar);
String MyString = MyDate.ToString("yyyy/MM/dd");
还有有效的邮箱地址,怎么判断输入语句中有且只有一个@和一个.啊?
/// <summary> /// 方法说明:验证日期时间否有效 /// </summary> private void button1_Click(object sender, EventArgs e) { if (RegexDateTimeValid(textBox1.Text.Trim())) { MessageBox.Show("Match"); } else { MessageBox.Show("No Match"); } } /// <summary> /// 方法说明:验证邮箱是否有效 /// </summary> private void button2_Click(object sender, EventArgs e) { if (RegexMailValid(textBox1.Text.Trim())) { MessageBox.Show("Match"); } else { MessageBox.Show("No Match"); } } /// <summary> /// 方法说明:验证日期时间是否有效 /// </summary> /// <param name="str">需要验证的字符串</param> /// <returns>True/False</returns> private bool RegexDateTimeValid(string str) { /* 可以匹配如下格式 12:30 PM 2012-02-18 2012/2/18 02:31:35 2012-02-18 15:44:45 2012/2/12 02:31:35 AM */ string match = @"^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\2(?'day'((?<!(\2((0?[2469])|11)\2))31)|(?<!\2(0?2)\2)(29|30)|((?<=((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\2\3\2)29)|((0?[1-9])|(1\d)|(2[0-8])))(?:(?=\x20\d)\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}))?)$"; if (Regex.IsMatch(str, match)) { return true; } else { return false; } } /// <summary> /// 方法说明:验证邮箱是否有效 /// </summary> /// <param name="str">需要验证的字符串</param> /// <returns>True/False</returns> private bool RegexMailValid(string str) { string match = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; if (Regex.IsMatch(str, match)) { return true; } else { return false; } }