📄 zipcodevalidator.java
字号:
/*
* Created on May 22, 2004
*
*/
package com.arcmind.jsfquickstart.validation;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Richard Hightower
*
*/
public class ZipCodeValidator implements Validator{
private boolean plus4Required;
private boolean plus4Optional;
/** Accepts zip codes like 85710 */
private static final String ZIP_REGEX = "[0-9]{5}";
/** Accepts zip code plus 4 extensions like "-1119" or " 1119" */
private static final String PLUS4_REQUIRED_REGEX = "[ |-]{1}[0-9]{4}";
/** Optionally accepts a plus 4 */
private static final String PLUS4_OPTIONAL_REGEX = "([ |-]{1}[0-9]{4})?";
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
/* Create the correct mask */
Pattern mask = null;
initProps(component);
if (plus4Required){
mask = Pattern.compile(ZIP_REGEX + PLUS4_REQUIRED_REGEX);
} else if (plus4Optional){
mask = Pattern.compile(ZIP_REGEX + PLUS4_OPTIONAL_REGEX);
} else if (plus4Required && plus4Optional){
throw new IllegalStateException("Plus 4 is either optional or required");
}
else {
mask = Pattern.compile(ZIP_REGEX);
}
/* Get the string value of the current field */
String zipField = (String)value;
/* Check to see if the value is a zip code */
Matcher matcher = mask.matcher(zipField);
if (!matcher.matches()){
FacesMessage message = new FacesMessage();
message.setDetail("Zip code not valid");
message.setSummary("Zip code not valid");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
/**
*
*/
private void initProps(UIComponent component) {
Boolean optional = Boolean.valueOf((String) component.getAttributes().get("plus4Optional"));
Boolean required = Boolean.valueOf((String) component.getAttributes().get("plus4Required"));
plus4Optional = optional==null ? plus4Optional : optional.booleanValue();
plus4Required = required==null ? plus4Optional : required.booleanValue();
}
/**
* @param plus4Optional The plus4Optional to set.
*/
public void setPlus4Optional(boolean plus4Optional) {
this.plus4Optional = plus4Optional;
}
/**
* @param plus4Required The plus4Required to set.
*/
public void setPlus4Required(boolean plus4Required) {
this.plus4Required = plus4Required;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -