14 注册字段验证

添加一个表单UserForm模型,用于存储表单的错误信息

提供一个validate方法用于校验所有字段

提供所有属性的get/set方法

 

简单搭建

package com.xue.form;

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

public class UserForm {
	private String username;
	private String password;
	private String repassword;
	private String email;
	private String birthday;
	Map<String, String> err = new HashMap<String, String>();

	public void validate() {
		if (username == null || username.trim().length() == 0) {
			err.put("username", "用户名不能为空");
		}
	}

改写RegisterServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		
		UserForm uForm = new UserForm();
		try {
			BeanUtils.populate(uForm, request.getParameterMap());
			uForm.validate();
			
			if(uForm.getErr().size()>0){
				System.out.println(uForm.getErr());
				return;
			}			
		} catch (IllegalAccessException | InvocationTargetException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

完整写

public class UserForm {
	private String username;
	private String password;
	private String repassword;
	private String email;
	private String birthday;
	Map<String, String> err = new HashMap<String, String>();

	public boolean validate() {
		if (username == null || username.trim().length() == 0) {
			err.put("username", "用户名不能为空");
		}
		// w:[A-Za-Z_09]
		if (!username.matches("\\w{5,15}")) {
			err.put("username", "用户名长度5~15");
		}

		if (password == null || password.trim().length() == 0) {
			err.put("password", "密码不能为空");
		}
		if (!password.matches("\\w{6,9}")) {
			err.put("password", "密码长度6~9");
		}

		if (!password.equals(repassword)) {
			err.put("repassword", "两次密码需一致");
		}
		if (email == null || email.trim().length() == 0) {
			err.put("email", "邮箱不能为空");
		}
		if (!email.matches("^[A-Za-z0-9][\\w\\-\\.]{3,12}@([\\w\\-]+\\.)+[\\w]{2,3}$")) {
			err.put("email", "邮箱格式要正确");
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			sdf.parse(birthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			err.put("birthday", "日期格式2018-01-11");
		}
		return err.size()==0;
	}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		
		UserForm uForm = new UserForm();
		try {
			BeanUtils.populate(uForm, request.getParameterMap());
			uForm.validate();
			
			if(!uForm.validate()){
				System.out.println(uForm.getErr());
				return;
			}			
		} catch (IllegalAccessException | InvocationTargetException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值