弊端拦截所有事件
package com.xherp.common.validate;
import java.awt.Component;
import javax.swing.InputVerifier;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
// This program demonstrates the use of the Swing InputVerifier class.
// It creates two text fields; the first of the text fields expects the
// string "pass" as input, and will allow focus to advance out of it
// only after that string is typed in by the user.
public class PassVerifier extends InputVerifier {
/**
*
* 逻辑连接或
*/
public static String OR="|";
/**
* 验证是否为空
*/
public static String VALIDATENULL = "^$";
/**
* 验证为正浮点数
*
* @param validateStr
* @return
*/
public static String VALIDATEFLOATNUMBER = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$|^[1-9]\\d*";
/**
* 验证数字
*/
public static String VALIDATEDIGIT = "^[0-9]+$";
/**
* 验证中文字符
*/
public static String VALIDATECHINESE = "[\u4e00-\u9fa5]+";
/**
* 验证EMAIL
*/
public static String VALIDATEEMAIL = "^[a-zA-Z][a-zA-Z0-9_-]*@([a-zA-Z0-9_-]+\\.)+(com|gov|net|com\\.cn|edu\\.cn)$";
/**
* 验证为正整数
*/
public static String VALIDATEPOSITIVEINTEGER = "^[1-9]\\d*$";
/**
* 备注限制长度为30个字符
*
* @param str
* @return
*/
public static String VALIDATENOTE = "^[\u4e00-\u9fa5]{1,30}$";
//---------------------------------------------------邦定控件光标全事件拦截验证
private String pattern;//正则表达式
private String message="";//错误提示消息
private Component frame=null;
/**
* 无参构造
*/
public PassVerifier() {
this.pattern = pattern;
}
/**
* 没有具体提示消息
* @param pattern
*/
public PassVerifier(String pattern) {
this.pattern = pattern;
}
/**
* 有具体提示消息窗口
* @param pattern
* @param frame
* @param message
*/
public PassVerifier(Component frame,String pattern,String message) {
this.pattern = pattern;
this.frame=frame;
this.message=message;
}
/**
* 控件邦定验证
*/
@Override
public boolean verify(JComponent input) {
JTextField tf = (JTextField) input;
boolean result=tf.getText().trim().matches(pattern);
if (tf.getText().matches(pattern)) {
result= true;
}else{
result=false;
}
if(result==false){
JOptionPane.showMessageDialog(frame, "请检查光标所在的输入项内容是否合法:"+message);
}
return result;
}
/**
* 辅助验证
* @param pattern 正则
* @param target 内容
* @return
*/
public static boolean validateRegular(String pattern,String target){
if(target.trim().matches(pattern)){
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println("".matches(""));
}
//示例
// public void passVerifier() {
//
// PassVerifier a=new PassVerifier(this,PassVerifier.VALIDATENULL+"|"+PassVerifier.VALIDATECHINESE,"输入空或不是中文");
//
// textField.setInputVerifier(a);
//
//
//
// WindowListener l = new WindowAdapter() {
// public void windowClosing(WindowEvent e) {
// System.exit(0);
// }
// };
// addWindowListener(l);
// }
//
//---------------------------------------------------自定义验证
/**
* 正则验证
* @param pattern 正则
* @param target 内容
* @return
*/
public static String validateRegular(String pattern,String target,String message){
if(target.trim().matches(pattern)){
return message;
}
return "";
}
/**
* 备注限制长度为30个字符
* @param str
* @return
*/
public static String validateNote(String target,String message){
if(target.trim()!=null&&target.trim().length()>30){
return message;
}else{
return "";
}
}
/**
* 验证是否为空
*/
public static String validateNull(String target,String message){
if(target.trim() == null){
return message;
}
return "";
}
}