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

📄 mycustomvalidator.java

📁 struts的一些用的书籍
💻 JAVA
字号:
package app05f.validator;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;

public class MyCustomValidator implements Serializable {

	public static boolean validateSecurePassword(Object bean,
    ValidatorAction va, Field field,  ActionMessages errors,
    HttpServletRequest request) {

		char ch;
		boolean containNumeric = false;
		boolean containNonNumeric = false;
		boolean isValid = false;
		int length = 0;
  	String value = null;
  	
    if (isString(bean)) 
    	value = (String) bean;
    else 
    	value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    
    //Must have a minimum length
    length = Integer.parseInt(field.getVarValue("minLength"));
		int i=0;
    if (value.length() >= length) { 
	  	//Must contain numeric and non numeric
    	while (i < value.length() && !isValid) {            
    		ch = value.charAt(i);        	  	
    		if (isNumeric(ch)) 
      		containNumeric = true;
      	else 
      		containNonNumeric = true;
      	if (containNumeric && containNonNumeric) 
      		isValid = true;     	
      	i++;
			}
    }
    if (isValid) {
    	return true;
    } 
		else {
    	errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
      return false;                        
    }
	}   

 	protected static boolean isString(Object object) {
  	return (object==null) ? true : String.class.isInstance(object);
  }
    
	protected static boolean isNumeric(char ch) {
  	return (ch >= '0' && ch <= '9');
	}
}

⌨️ 快捷键说明

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