服务器端表单检验(非检验框架实现)

public class RegisterForm {
private String username;
private String password;
private String password2;
private String email;
private String birthday;
private String nickname;

private String client_checkcode;
private String server_checkcode;

private Map<String, String> errors = new HashMap<String, String>();


//用户名不能为空,并且要是3-8位字母
//密码不能为空,并且是3-8位数字
//确认密码不能为空,并且要和一次一致
//电子邮箱不能为空,并且要是一个格式合法的邮箱
//生日可以为空,不为空时,必须要是一个日期
//昵称不可以为空,并且要是汉字
public boolean validate() {
if (username == null || "".equals(username.trim())) {
errors.put("username", "用户名不能为空");
return false;
} else {
if (!username.matches("[a-zA-Z]{3,8}")) {
errors.put("username", "用户名必须为3到8位字母");
return false;
}
}

if (password == null || "".equals(password.trim())) {
errors.put("password", "密码不能为空");
return false;
} else {
if(!password.matches("\\d{3,8}")) {
errors.put("password", "密码必须为3到8位数字");
return false;
}
}

if (password2 == null || "".equals(password2.trim())) {
errors.put("password2", "确认密码不能为空");
return false;
} else {
if (!password2.equals(password)) {
errors.put("password2", "两次密码必须一致");
return false;
}
}

if(email==null || "".equals(email.trim())) {
errors.put("email", "邮箱不能为空");
return false;
} else {
if(!email.matches("\\w+@\\w+[\\.\\w]+")) { //sabc@sina.com
errors.put("email", "邮箱格式不正确");
return false;
}
}

if (birthday != null && !"".equals(birthday.trim())) {
try {
DateLocaleConverter dlc = new DateLocaleConverter();
dlc.convert(birthday, "yyyy-MM-dd");
} catch(Exception e) {
errors.put("birthday", "日期格式不正确");
return false;
}
}

if (nickname == null || "".equals(nickname.trim())) {
errors.put("nickname", "昵称不能为空");
return false;
} else {
if (!nickname.matches("^([\u4e00-\u9fa5]+)$")) {
errors.put("nickname", "昵称应为汉字");
return false;
}
}

if (client_checkcode == null || "".equals(client_checkcode.trim())) {
errors.put("client_checkcode", "必须输入验证码");
return false;
} else {
if (!server_checkcode.equals(client_checkcode)) {
errors.put("client_checkcode", "验证码正确");
return false;
}
}
return true;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword2() {
return password2;
}

public void setPassword2(String password2) {
this.password2 = password2;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public String getClient_checkcode() {
return client_checkcode;
}

public void setClient_checkcode(String client_checkcode) {
this.client_checkcode = client_checkcode;
}

public String getServer_checkcode() {
return server_checkcode;
}

public void setServer_checkcode(String server_checkcode) {
this.server_checkcode = server_checkcode;
}

public Map<String, String> getErrors() {
return errors;
}

public void setErrors(Map<String, String> errors) {
this.errors = errors;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值