![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
代码namespace 用户登录 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool b1, b2, b3, b4, b5, b6; private void button1_Click(object sender, EventArgs e) { try { if (b1 && b2 && b3 && b4 && b5 && b6) { using (SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=MyLogin;Integrated Security=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"insert into T_userinformation (userName, userPWD, userPhone, userEmial, userBirthday) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox6.Text + "')"; cmd.ExecuteNonQuery(); } } MessageBox.Show("注册成功"); } else { MessageBox.Show("请完善信息"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void textBox1_Leave(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text.Trim())) { return; } b1 = Regex.IsMatch(textBox1.Text.Trim(), @"^\w{0,20}$"); using (SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=MyLogin;Integrated Security=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select count(userName) from T_userinformation where userName='" + textBox1.Text.Trim() + "'"; int res = Convert.ToInt32(cmd.ExecuteScalar()); if (b1) { if (res == 0) { labelUid.ForeColor = Color.Green; labelUid.Text = "恭喜,用户名可以使用!"; } else { labelUid.ForeColor = Color.Red; labelUid.Text = "用户名已存在"; } } else { labelUid.ForeColor = Color.Red; labelUid.Text = "用户名长度超过20"; } } } } private void textBox3_Leave(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox3.Text.Trim())) { return; } b2 = Regex.IsMatch(textBox3.Text.Trim(), @"^\d{6}$"); if (b2) { labelpas.ForeColor = Color.Green; labelpas.Text = "恭喜,密码输入正确!"; } else { labelpas.ForeColor = Color.Red; labelpas.Text = "输入错误,请检查密码长度和格式"; } } private void textBox2_Leave(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox2.Text.Trim())) { return; } b3 = textBox2.Text.Trim() == textBox3.Text.Trim(); if (b3) { labelpas2.ForeColor = Color.Green; labelpas2.Text = "恭喜,密码验证正确!"; } else { labelpas2.ForeColor = Color.Red; labelpas2.Text = "输入错误,请检查密码"; } } private void textBox4_Leave(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox4.Text.Trim())) { return; } b4 = Regex.IsMatch(textBox4.Text.Trim(), @"^[0-9a-zA-Z\.-_]+@[0-9a-zA-Z\-_]+(\.[0-9a-zA-Z\-_]+)+$"); if (b4) { labelmail.ForeColor = Color.Green; labelmail.Text = "恭喜,邮箱输入正确!"; } else { labelmail.ForeColor = Color.Red; labelmail.Text = "输入错误,请检查邮箱格式"; } } private void textBox5_Leave(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox5.Text.Trim())) { return; } b5 = Regex.IsMatch(textBox5.Text.Trim(), @"^\d{11}$"); if (b5) { labelphone.ForeColor = Color.Green; labelphone.Text = "恭喜,手机输入正确"; } else { labelphone.ForeColor = Color.Red; labelphone.Text = "输入错误,请检查手机格式"; } } private void textBox6_Leave(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox6.Text.Trim())) { return; } b6 = Regex.IsMatch(textBox6.Text.Trim(), @"^\d{4}\-((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))$"); if (b6) { labelbirth.ForeColor = Color.Green; labelbirth.Text = "恭喜,出生日期输入正确"; } else { labelbirth.ForeColor = Color.Red; labelbirth.Text = "输入错误,请检查出生日期格式"; } } } }