数据验证

import java.util.Date;

import java.util.List;

public class ValidateUtil {

public static final String DATE_FORMAT_1 = "yyyy/MM/dd";
public static final String DATE_FORMAT_2 = "MM/dd/yyyy";
public static final String DATE_FORMAT_3 = "dd/MM/yyyy";
public static final String DATE_FORMAT_4 = "yyyy/MM/dd hh:mm:ss";
public static final String DATE_FORMAT_5 = "MM/dd/yyyy hh:mm:ss";
public static final String DATE_FORMAT_6 = "dd/MM/yyyy hh:mm:ss";
public static final String TIME_FORMAT_1 = "hh:mm:ss";
public static final String TIME_FORMAT_2 = "hh:mm";

public static boolean isEmail(String email) {
if (email == null || email.length() < 5)
return false;

String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
return email.matches(regex);
}

public static boolean isEmpty(String val) {

return val == null || val.trim().length() == 0;
}

public static boolean isEqual(String val1,String val2) {
if(val1 != null){
return val1.equals(val2);
}else if(val2 != null){
return val2.equals(val1);
}else{
return true;
}
}

/**
* Rule: length: >= 8 <= 20 characters, at least one upper case character, and one digit.
*/
public static boolean checkPassword(String password) {
if (password == null || password.length() < 8 || password.length() > 20)
return false;

char [] pwd = password.trim().toCharArray();
int c = 0;
for (int i = 0; i < pwd.length; i++) {
if ((int)pwd[i] >= 48 && (int)pwd[i] <= 57) {
c++;
break;
}
}
if(c==0){
return false;
}
c = 0;
for (int i = 0; i < pwd.length; i++)
{
if ((int)pwd[i] >= 65 && (int)pwd[i] <= 90)
{
c++;
break;
}
}
if (c == 0)
{
return false;
}

return true;
}

public static boolean checkPassword(String password,String regex) {
if (password == null || password.length() <= 0)
return false;

return password.matches(regex);
}

public static boolean isAtoZ(String val) {

if (val == null || val.length() <= 0)
return false;

String regex = "^[A-Za-z]+$";
return val.matches(regex);
}

public static boolean isNumber(String val) {
if (val == null || val.length() <= 0)
return false;

String regex = "^-?([1-9]\\d*\\.?\\d*|0\\.?\\d*[1-9]\\d*|0?\\.?0+|0)$";
return val.matches(regex);

}

public static boolean isInteger(String val) {

if (val == null || val.length() <= 0)
return false;

String regex = "^-?[1-9]\\d*$";
return val.matches(regex);
}

public static boolean isPositiveInteger(String val) {

if (val == null || val.length() <= 0)
return false;

String regex = "^[1-9]\\d*$";
return val.matches(regex);
}

public static boolean isDate(String date) {
if (date == null)
return false;

String regex = "^[0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)$";
if(date.matches(regex)){
return true;
}

regex = "^((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";
if(date.matches(regex)){
return true;
}

regex = "^((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";
if(date.matches(regex)){
return true;
}

regex = "([0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^(((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^(((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if(date.matches(regex)){
return true;
}

return false;
}

public static boolean isDate(String date, String format) {
String regex = "";
if(DATE_FORMAT_1.equals(format)){
regex = "^[0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)$";
}else if(DATE_FORMAT_2.equals(format)){
regex = "^((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";
}else if(DATE_FORMAT_3.equals(format)){
regex = "^((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$";

}else if(DATE_FORMAT_4.equals(format)){
regex = "([0-9]{4}/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8])))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
}else if(DATE_FORMAT_5.equals(format)){
regex = "^(((((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|[2][0-8])))/[0-9]{4})|(02/29/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
}else if(DATE_FORMAT_6.equals(format)){
regex = "^(((((0[1-9]|[12][0-9]|3[01]))/(0[13578]|1[02])|((0[1-9]|[12][0-9]|30)/(0[469]|11))|((0[1-9]|[1][0-9]|[2][0-8]))/02)/[0-9]{4})|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";

}else if(TIME_FORMAT_1.equals(format)){
regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
}else if(TIME_FORMAT_2.equals(format)){
regex = "^([0-1][0-9]|2[0-3]):([0-5][0-9])$";
}else{
return false;
}

return date.matches(regex);
}

public static int compareDate(Date date1, Date date2) throws NullPointerException{

if(date1 == null || date2 == null)
throw new NullPointerException();

return date1.compareTo(date2);
}

public static boolean minlength(String str, Integer compareVal) {
if (str == null)
return false;

return str.length() >= compareVal;
}

public static boolean maxlength(String str, Integer compareVal) {

if (str == null)
return false;

return str.length() <= compareVal;
}

public static boolean rangeLength(String str, Integer min, Integer max) {
if (str == null)
return false;

return str.length() >= min && str.length() <= max;
}

public static boolean rangeValue(int val, int min, int max) {

return val >= min && val <= max;
}

public static boolean rangeValue(double val, double min, double max) {
return val >= min && val <= max;
}

public static boolean rangeValue(char chars, char min, char max) {

return chars >= min && chars <= max;
}

public static boolean isExist(String val,String [] source) {

if(source == null || source.length == 0)
return false;

for(String str : source){
if(str != null && str.equals(val)){
return true;
}
}

return false;
}

public static boolean isExist(String val,List<String> source) {

if(source == null || source.size() == 0)
return false;

for(String str : source){
if(str != null && str.equals(val)){
return true;
}
}

return false;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值