1 /// <summary> 2 /// 验证 非空 3 /// </summary> 4 /// <param name="isOK"></param> 5 /// <param name="cons"></param> 6 private void CheckControls(ref bool isOK,params Control[] cons) 7 { 8 9 foreach(Control con in cons) 10 { 11 //非空验证 12 if(con is TextBox) 13 { 14 if (con.Text.Trim().Length <= 0) 15 { 16 con.BackColor = System.Drawing.Color.Red; 17 isOK = false; 18 } 19 else 20 { 21 con.BackColor = System.Drawing.Color.White; 22 } 23 } 24 } 25 }
1 /// <summary> 2 /// 保存 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnSave_Click(object sender, EventArgs e) 7 { 8 #region 验证 非空 9 //验证 非空 10 bool isOk = true; 11 CheckControls(ref isOk, txtMobile, txtPwd, txtUserName); 12 if (!isOk) 13 { 14 return; 15 } 16 #endregion 17 18 #region 验证 用户名已存在 19 //验证 用户名已存在 20 string username = txtUserName.Text.Trim(); 21 UserInfo user1 = userBll.GetUserInfoByUserName(username); 22 if (user1 != null) 23 { 24 lbUserName.Text = "用户名已存在"; 25 return; 26 } 27 lbUserName.Text = ""; 28 #endregion 29 30 #region 验证 密码强度 31 //验证 密码强度 32 if (txtPwd.Text.Trim().Length < 6) 33 { 34 lbPwd.Text = "密码至少六位数字或英文字母"; 35 return; 36 } 37 lbPwd.Text = ""; 38 #endregion 39 40 #region 验证 手机号 41 //验证 手机号 42 //必须是11位,必须是1开始,必须每一位都是数字 43 if (txtMobile.Text.Trim().Length != 11) 44 { 45 lbMobile.Text = "必须是11位"; 46 return; 47 } 48 if (txtMobile.Text.Substring(0, 1) != "1") 49 { 50 lbMobile.Text = "必须以1开始"; 51 return; 52 } 53 foreach (char ch in lbMobile.Text.Trim().ToCharArray()) 54 { 55 if (!char.IsDigit(ch)) 56 { 57 lbMobile.Text = "必须全是数字"; 58 return; 59 } 60 } 61 lbMobile.Text = ""; 62 #endregion 63 64 #region 验证 邮箱 65 //验证 邮箱 66 if(txtEmail.Text.Trim().Length>0) 67 { 68 //[0-9a-zA-Z_.-]+@[0-9z-zA-Z_.-]+([.][a-zA-Z]){1,2} 69 //^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$ 70 string reg = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"; 71 if (!Regex.IsMatch(txtEmail.Text.Trim(), reg)) 72 { 73 lbEmail.Text = "请输入符合邮箱格式的邮箱"; 74 return; 75 } 76 } 77 lbEmail.Text = ""; 78 #endregion 79 80 //赋值 81 UserInfo user = new UserInfo(); 82 user.Address = txtAddress.Text; 83 user.Email = txtEmail.Text; 84 user.Mobile = txtMobile.Text; 85 user.Pwd = EncryptPwd.EncryptPassword(txtPwd.Text + EncryptPwd.GetPwdSalt()); //(密码+密码盐)之后再 加密 86 user.UserName = txtUserName.Text; 87 user.Gender = rdoMale.Checked ? 1 : 2; //rdo必须限制有且只有一项选中 88 bool flag = false; 89 if (this.isAdd == 1) //新增 90 { 91 //新增 92 flag = userBll.InsertUserInfo(user); 93 } 94 else if (this.isAdd == 2) //编辑 95 { 96 user.Id = Convert.ToInt32(lbId.Text); 97 flag = userBll.UpdateUserInfo(user); 98 } 99 else 100 { 101 throw new Exception("未知的isAdd值:"+this.isAdd); 102 } 103 string msg = flag ? "操作成功" : "操作失败"; 104 MessageBox.Show(msg); 105 //DialogResult = DialogResult.OK; 106 this.Close(); 107 }