1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 邮箱日期IP地址信用卡验证(正则表达式)

邮箱日期IP地址信用卡验证(正则表达式)

时间:2021-05-05 14:15:48

相关推荐

邮箱日期IP地址信用卡验证(正则表达式)

使用正则表达式匹配验证,存在不足,希望大家提供更优的验证表达式

package com.qingbyqing.job;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class FormValid {

public static void w(Object o) {

System.out.println(o);

}

/**

* 邮箱验证:

*/

public static boolean isValidEmail(String email) {

String regEmail = "^(?:\\w+\\.{1})*\\w+@(\\w+\\.)*\\w+$";

Pattern pat = pile(regEmail);

Matcher mat = pat.matcher(email);

if (mat.find()) {

w("合法邮箱");

return true;

}

w("邮箱格式错误!");

return false;

}

/**

* ip 地址的验证

*

*/

public static boolean isValidIp(String strIp) {

String reIp = "\\b((\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])(\\b|\\.)){4}";

Pattern com = pile(reIp);

Matcher mat = com.matcher(strIp);

if (mat.find()) {

w("IP地址格式正确");

return true;

}

w("Ip地址格式错误");

return false;

}

/**

* 验证日期

*

*/

public static boolean isValidDate(String sDate) {

String reDate = "(?:[1-9]|0[1-9]|[12]\\d|3[0-1])(\\/|\\-)(?:[1-9]|0[1-9]|1[0-2])(\\/|\\-)(?:19|20\\d\\d)";

Pattern com = pile(reDate);

Matcher mat = com.matcher(sDate);

if (mat.find()) {

w("日期格式正确");

return true;

}

w("日期格式错误");

return false;

}

/**

* 验证信用卡号

*

*/

public static boolean isValidCard(String sCard) {

String reCard = "^(4\\d{12}(?:\\d{3})?)$";

Pattern com = pile(reCard);

Matcher mat = com.matcher(sCard);

if (mat.find()) {

w(reCard);

w("格式正确");

// 判断是否合法

boolean luhn = FormValid.isLuhn(sCard);

if (luhn) {

w("卡号是合法的");

return true;

} else {

w("卡号不合法");

return false;

}

}

w("格式不正确");

return false;

}

/**

* luhn算法

*

*/

public static boolean isLuhn(String strNum){

int oddSum=0;

int evenSum=0;

boolean isOdd=true;

for (int i=strNum.length()-1;i>=0;i--){

char cNum=strNum.charAt(i);

int num=Integer.parseInt(cNum+"");

System.out.print("第"+i+"个"+"是"+"\t"+num+"\n");

if(isOdd){

oddSum+=num;

}else{

num=num*2;

if(num>9){

num=num%10+1;

}

evenSum=evenSum+num;

}

isOdd=!isOdd;

}

return ((evenSum+oddSum)%10==0);

}

/**

* 测试

*

*/

public static void main(String args[]) {

String email = "qing.qingbyqing@";//邮箱测试

FormValid.isValidEmail(email);

String strIp="1.10.111.255";//IP地址测试

FormValid.isValidIp(strIp);

String sDate="03/03/1911";

FormValid.isValidDate(sDate);//日期测试

String strNum = "4432123456788881";

w(FormValid.isValidCard(strNum));//信用卡测试

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。