⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customvalidator.java

📁 IBM RSA下的JSF开发示例
💻 JAVA
字号:
/* CustomValidator.java
 * Created on Jan 26, 2005
 *
 * A custom validator class which checks to see if a number value is an even number between 2 and 100,000.
 */
package com.ibm.samples.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;

/**
 * @author cmjaun
 *
 */
public class CustomValidator implements Validator {

	/* (non-Javadoc)
	 * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		if(value == null) return;
		int val = 0;
		try {
			val = Integer.parseInt(value.toString());
			if(val % 2 != 0 || val > 100000 || val < 2 ){
				FacesMessage msg = new FacesMessage("Error: Please enter an Even number between 2 and 100,000.");
				throw new ValidatorException(msg);
			}
		} catch(NumberFormatException e) {
			FacesMessage msg = new FacesMessage("Error: Please enter an Even number between 2 and 100,000.");
			throw new ValidatorException(msg);
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -