在做项目的时候,输入信息相关的地方少不了要进行信息校验,我总结了一下我用到的校验方式
电话 (11位号码)如:135********
public static boolean regexPhoneNumber(String phoneNum) {
// 电话号码匹配结果
boolean isPhoneNum_matcher = phoneNum.matches("1[34578]\\d{9}");
// 如果isPhoneNum_matcher is true , 则return true , else return false
if (isPhoneNum_matcher)
return true;
return false;
}
邮箱
/**
* 邮箱正则验证:"[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,}){1,3}" '@'号前没有点'.'
* 邮箱正则验证:"^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$" '@'号前有点'.'
*
* param email
* 传入的参数仅仅是一个邮箱地址时,调用此方法
* return 如果匹配正确,return true , else return false
*/
// 如果传进来的是邮箱地址,则对邮箱进行正则匹配
public static boolean regexEmailAddress(String email) {
// 邮箱匹配结果
boolean isEmail_matcher = email.matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
// 如果isEmail_matcher value is true , 则 return true , else return false
if (isEmail_matcher)
return true;
return false;
}
/**
*
* param qqNum
* 传入的QQ
* return 如果匹配正确,return true, else return false
*/
public static boolean regexQQNumber(String qqNum) {
// QQ号匹配结果
boolean isQQNum_matcher = qqNum.matches("[1-9]\\d{2,11}");
if (isQQNum_matcher)
return true;
return false;
}
邮编
public static boolean checkpostalcode(String postalcode) {
boolean ispostalcode_matcher = postalcode.matches("[1-9]\\d{5}(?!\\d)");
if (ispostalcode_matcher)
return true;
return false;
}
IC Card
public static boolean checkICCard(String cardNumber) {
boolean iscardNumber_matcher = cardNumber.matches("[1-9]\\d{5}(?!\\d)");
if (iscardNumber_matcher)
return true;
return false;
}
对于6到20位包含几种特殊字符(!#%*等)密码 的验证
public static boolean regexPassWord(String pwd) {
// 密码匹配结果
// boolean isPassWord_matcher = pwd.matches("^([A-Z]|[a-z]|[0-9]){6,20}$");
//^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$
boolean isPassWord_matcher = pwd.matches("^[\\@A-Za-z0-9\\!\\#\\$\\%\\^\\&\\*\\.\\~]{6,20}$");
if (isPassWord_matcher)
return true;
return false;
}