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

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]+")) { //[email protected]
				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;
	}
}

猜你喜欢

转载自cookieandsession.iteye.com/blog/1921924
今日推荐