java 数据验证类
来源:互联网 发布:最新网络流行歌曲2017 编辑:程序博客网 时间:2024/06/05 09:37
package com.cnpc.xjddc.common;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dataValidate {
/**
* 验证邮箱
*
* @param 待验证的字符串
* @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean isEmail(String str) {
String regex = "^([//w-//.]+)@((http://www.ccc.com/yaojian/admin/file://[[0-9]%7b1,3%7d//.[0-9]%7B1,3%7D//.[0-9]%7B1,3%7D//.)%7C(([//w-]+//.)+))([a-zA-Z]%7B2,4%7D%7C[0-9]%7B1,3%7D)(//]?)$";
return match(regex, str);
}
/**
* 验证IP地址
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean isIP(String str) {
String num = "(25[0-5]|2[0-4]//d|[0-1]//d{2}|[1-9]?//d)";
String regex = "^" + num + "//." + num + "//." + num + "//." + num + "$";
return match(regex, str);
}
/**
* 验证网址Url
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsUrl(String str) {
String regex = "http(s)?://([//w-]+//.)+[//w-]+(/[//w- ./?%&=]*)?";
return match(regex, str);
}
/**
* 验证电话号码
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsTelephone(String str) {
String regex = "^(http://www.ccc.com/yaojian/admin/file://d%7b3,4%7d-)/?//d{6,8}$";
return match(regex, str);
}
/**
* 验证输入密码条件(字符与数据同时出现)
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsPassword(String str) {
String regex = "[A-Za-z]+[0-9]";
return match(regex, str);
}
/**
* 验证输入密码长度 (6-18位)
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsPasswLength(String str) {
String regex = "^//d{6,18}$";
return match(regex, str);
}
/**
* 验证输入邮政编号
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsPostalcode(String str) {
String regex = "^//d{6}$";
return match(regex, str);
}
/**
* 验证输入手机号码
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsHandset(String str) {
String regex = "^[1]+[3,5]+//d{9}$";
return match(regex, str);
}
/**
* 验证输入身份证号
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsIDcard(String str) {
String regex = "(^//d{18}$)|(^//d{15}$)";
return match(regex, str);
}
/**
* 验证输入两位小数
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsDecimal(String str) {
String regex = "^[0-9]+(.[0-9]{2})?$";
return match(regex, str);
}
/**
* 验证输入一年的12个月
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsMonth(String str) {
String regex = "^(0?[[1-9]|1[0-2])$";
return match(regex, str);
}
/**
* 验证输入一个月的31天
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsDay(String str) {
String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
return match(regex, str);
}
/**
* 验证日期时间
*
* @param 待验证的字符串
* @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean isDate(String str) {
// 严格验证时间格式的(匹配[2002-01-31], [1997-04-30],
// [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])
// String regex =
// "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1//d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";
// 没加时间验证的YYYY-MM-DD
// String regex =
// "^((((1[6-9]|[2-9]//d)//d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]//d|3[01]))|(((1[6-9]|[2-9]//d)//d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]//d|30))|(((1[6-9]|[2-9]//d)//d{2})-0?2-(0?[1-9]|1//d|2[0-8]))|(((1[6-9]|[2-9]//d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
// 加了时间验证的YYYY-MM-DD 00:00:00
String regex = "^((((1[6-9]|[2-9]//d)//d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]//d|3[01]))|(((1[6-9]|[2-9]//d)//d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]//d|30))|(((1[6-9]|[2-9]//d)//d{2})-0?2-(0?[1-9]|1//d|2[0-8]))|(((1[6-9]|[2-9]//d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?//d):[0-5]?//d:[0-5]?//d$";
return match(regex, str);
}
/**
* 验证数字输入
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsNumber(String str) {
String regex = "^[0-9]*$";
return match(regex, str);
}
/**
* 验证非零的正整数
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsIntNumber(String str) {
String regex = "^//+?[1-9][0-9]*$";
return match(regex, str);
}
/**
* 验证大写字母
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsUpChar(String str) {
String regex = "^[A-Z]+$";
return match(regex, str);
}
/**
* 验证小写字母
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsLowChar(String str) {
String regex = "^[a-z]+$";
return match(regex, str);
}
/**
* 验证输入字母
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsLetter(String str) {
String regex = "^[A-Za-z]+$";
return match(regex, str);
}
/**
* 验证输入汉字
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsChinese(String str) {
String regex = "^[/u4e00-/u9fa5],{0,}$";
return match(regex, str);
}
/**
* 验证输入字符串
*
* @param 待验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IsLength(String str) {
String regex = "^.{8,}$";
return match(regex, str);
}
/**
* @param regex
* 正则表达式字符串
* @param str
* 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
private static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
}
- java 数据验证类
- java 数据验证类
- java 验证增加数据
- java数据验证
- Java身份证号码数据验证
- spring数据验证----验证类
- 【转】java类中的数据验证(正则表达式)
- Struts2数据验证与使用Java代码进行数据验证
- Javascript数据验证类
- JAVA中进行数据完整性验证
- java 后台验证数据唯一性
- java框架之springmvc06(数据验证)
- Java 验证请求数据完整性信息
- C#-通用数据验证类
- java验证码类
- JAVA验证类
- java身份证验证类
- Java 开发验证类
- Don Norman: 简单不是答案
- Access学习编程中
- 餐馆联邦一次运行的时间推进序列 (按时间排序的时间推进请求和time grant序列)
- ABPA 对文件的存取
- struts1.x 文件上传 formfile
- java 数据验证类
- 测试株连分析(Test Impact)
- [软件人生]青润的职业定位
- [继续摘抄]架构师给程序员的一封信
- 使用SkinMagic Toolkit美化界面
- C++中实现委托(Delegate)
- 判断指定文件夹是否存在
- J2ME应用程序开发奇技淫巧(上、下)
- Delphi中带缓存的数据更新技术