把前台的form表单提交内容封装成formbean,并对form中的文本框进行校验

package cn.domain;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

public class formbean {
 private String userName;
 private String nickName;
 private String mail;
 private String birthday;
 private String password;
 private String password2;

//可能各个文本框可能出现的文本错误封装到map集合中比较好取
 private Map errorsmap = new HashMap();

 public boolean confirm() {
  boolean isOk = true;
  if (userName == null && userName.trim().equals("")) {
   isOk = false;
   errorsmap.put("userName", "用户名不能是空");
  } else {
   if (!userName.matches("[a-zA-Z](3,8)")) {
    isOk = false;
    errorsmap.put("userName", "用户名为3-8位字母!");
   }
  }

  if (password == null && password.trim().equals("")) {
   isOk = false;
   errorsmap.put("password", "密码不能是空的");
  } else {
   if (!password.matches("
\\d(3,8)")) {
    isOk = false;
    errorsmap.put("password", "密码必须是3-8位数字");
   }
  }
  if (password2 == null && password2.trim().equals("")) {
   isOk = false;
   errorsmap.put("password2", "重新密码不能是空的");
  } else {
   if (!password.matches("
\\d(3,8)")) {
    isOk = false;
    errorsmap.put("password2", "密码必须是3-8位数字");
   }
  }
  if (password != password2) {
   isOk = false;
   errorsmap.put("password2", "两次输入的密码不一样");
  }

  if (mail == null && mail.trim().equals("")) {
   isOk = false;
   errorsmap.put("mail", "邮箱不能是空的");
  } else {
   if (!mail.matches("
\\w+@\\w+(\\.\\w+)+")) {
    isOk = false;
    errorsmap.put("mail", "邮箱格式不正确");
   }
  }
  // 昵称是中文的,常用汉字范围是"[\u4e00-\u9fa5]+"
  if (nickName == null && nickName.trim().equals("")) {
   isOk = false;
   errorsmap.put("nickName", "昵称不能是空的!");
  } else {
   if (!mail.matches("[\u4e00-\u9fa5]+")) {
    isOk = false;
    errorsmap.put("nickName", "昵称必须是汉字");
   }
  }
  // 日期的格式必须是1990-12-15形式,使用
  if (nickName == null && nickName.trim().equals("")) {
   try {
    DateLocaleConverter dlc = new DateLocaleConverter();
    dlc.convert(this.birthday, "yyyy-MM-dd");
   } catch (Exception e) {
    isOk = false;
    errorsmap.put("birthday", "生日格式输入错误,1990-12-15形式");
   }
  }
  return isOk;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getNickName() {
  return nickName;
 }

 public void setNickName(String nickName) {
  this.nickName = nickName;
 }

 public String getMail() {
  return mail;
 }

 public void setMail(String mail) {
  this.mail = mail;
 }

 public String getBirthday() {
  return birthday;
 }

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

 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;
 }

}

你可能感兴趣的:(form表单校验)