TA的每日心情 | 开心 2021-3-12 23:18 |
|---|
签到天数: 2 天 [LV.1]初来乍到
|
|
这是一个用正则表达式和bean进行表单验证的jsp例子。
带表单的jsp文件(请观看演示并下载源文件)
下面是验证bean,validateBean.java
package examples;
import java.util.*;
import java.text.*;
import java.util.Date;
import java.util.regex.*;
public class validateBean {
private String title;
private String marriedFlag;
private String hobbies[];
private String colors[];
private String ageGroup;
private String telephoneNumber;
private String birthDate;
private String ssn;
private String email;
private String comments;
private SimpleDateFormat dateFormat;
private String DATE_FORMAT_PATTERN;
private Hashtable errors;
public boolean validate() {
boolean errorsFound=false;
if (title.equals("")) {
errors.put("title","标题不能为空");
errorsFound=true;
}
if (telephoneNumber.equals("")) {
errors.put("telephoneNumber","Please enter a valid telephone #");
errorsFound=true;
}
else {
if (!(telephoneNumber.matches("\+?([0-9]+-)+([0-9]+-)+[0-9]+"))) {
errors.put("telephoneNumber",
"Please enter a valid telephone format ### - ### - ####");
errorsFound=true;
}
}
if (ssn.equals("")) {
errors.put("ssn","Please enter a valid Social Security #");
errorsFound=true;
}
else {
if (!(ssn.matches("(\d{3}\-?)+(\d{2}\-?)+\d{4}+"))) {
errors.put("ssn","Please enter a valid SSN: ### - ## - ####");
errorsFound=true;
}
}
if (birthDate.equals("")) {
errors.put("birthDate","Please enter a valid date");
errorsFound=true;
}
else {
if (!(birthDate.matches("(\d{4}\-?)+(\d{2}\-?)+\d{2}+"))) {
errors.put("birthDate","Please enter a valid date format: (yyyy-mm-dd)");
errorsFound=true;
}
/*
Date date=null;
try
{
dateFormat.applyPattern(DATE_FORMAT_PATTERN);
// dateFormat.setLenient(false);
date = dateFormat.parse(birthDate);
}
catch(ParseException parseexception) { }
if (date==null) {
errors.put("birthDate","Please enter a valid date format: (yyyy-mm-dd)");
birthDate="";
errorsFound=true;
}
*/
}
if (email.equals("")) {
errors.put("email","Please enter a valid email address");
errorsFound=true;
}
else {
if (!(email.matches("[-A-Za-z0-9_.]+@[-A-Za-z0-9_.]+\.[-A-Za-z]{2,}"))) {
errors.put("email","Please enter a valid email address: ex. name@company.org");
errorsFound=true;
}
}
if (comments.equals("")) {
errors.put("comments","Please enter a comment");
errorsFound=true;
} else {
Pattern pattern = Pattern.compile("(darn|shoot|damn|jerk|stupid|dummy)");
Matcher match = pattern.matcher(comments);
if (match.find()) { comments = match.replaceAll("#%&@"); }
}
return errorsFound;
}
public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}
public validateBean() {
title="";
marriedFlag="1";
hobbies = new String[] { "" };
colors = new String[] { "" };
telephoneNumber="";
birthDate="";
ssn="";
email="";
comments="";
dateFormat = new SimpleDateFormat();
DATE_FORMAT_PATTERN = "yyyy-MM-dd";
errors = new Hashtable();
}
// getters
public String getTitle() {
return title;
}
public String getMarriedFlag() {
return marriedFlag;
}
public String getTelephoneNumber() {
return telephoneNumber;
}
public String getBirthDate() {
return birthDate;
}
public String getSsn() {
return ssn;
}
public String getAgeGroup() {
return ageGroup;
}
public String[] getColors() {
return colors;
}
public String[] getHobbies() {
return hobbies;
}
public String getEmail() {
return email;
}
public String getComments() {
return comments;
}
// setters
public void setTitle(String t) {
title=t;
}
public void setMarriedFlag(String mf) {
marriedFlag=mf;
}
public void setTelephoneNumber(String tn) {
telephoneNumber=tn;
}
public void setBirthDate(String bd) {
birthDate=bd;
}
public void setSsn(String s) {
ssn=s;
}
public void setAgeGroup(String ag) {
ageGroup=ag;
}
public void setColors(String[] c) {
colors=c;
}
public void setHobbies(String[] h) {
hobbies=h;
}
public void setEmail(String e) {
email=e;
}
public void setComments(String c) {
comments=c;
}
public String hobbySelected(String s) {
boolean selected=false;
for (int i = 0; i < hobbies.length; i++) {
if (hobbies.equals(s)) {
return "selected";
}
}
return "";
}
public String colorSelected(String s) {
boolean selected=false;
for (int i = 0; i < colors.length; i++) {
if (colors.equals(s)) {
return "checked";
}
}
return "";
}
public String marriedSelected() {
return (marriedFlag.equals("true")) ? "checked" : "";
}
public String ageGroupSelected(String s) {
return (ageGroup.equals(s)) ? "selected" : "";
}
}
源码下载:http://file.javaxxz.com/2014/11/3/000221437.zip |
|