JAVA、JS中验证身份证号码、手机号码、电子邮箱的正则表达式 (一)

2014-11-24 11:10:38 · 作者: · 浏览: 2

JAVA中:[java]
Pattern patternSfzhm1 = Pattern
.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$");
Pattern patternSfzhm2 = Pattern
.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");
Matcher matcherSfzhm1 = patternSfzhm1.matcher(sfzhm);
Matcher matcherSfzhm2 = patternSfzhm2.matcher(sfzhm);
Pattern patternSjhm = Pattern
.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher matcherSjhm = patternSjhm.matcher(sjhm);
Pattern patternDzyx = Pattern
.compile("^([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})(\\] )$");
Matcher matcherDzyx = patternDzyx.matcher(dzyx);
if (xm.equals("")) {
showToast("姓名不能为空!");
}
else if (dlzh.equals("")) {
showToast("登录账号不能为空!");
}
else if (dlmm.equals("")) {
showToast("登录密码不能为空!");
}
else if (sfzhm.equals("")) {
showToast("身份证号码不能为空!");
}
else if (!(sfzhm.equals("")) && !matcherSfzhm1.find() && !matcherSfzhm2.find()) {
showToast("身份证号码格式不正确,请重新输入!");
}
else if(!(sjhm.equals("")) && !(matcherSjhm.find())) {
showToast("手机号码格式不正确,请重新输入!");
}
else if(!(dzyx.equals("")) && !((matcherDzyx.find()))) {
showToast("电子邮箱格式不正确,请重新输入!");
}

Pattern patternSfzhm1 = Pattern
.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$");
Pattern patternSfzhm2 = Pattern
.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");
Matcher matcherSfzhm1 = patternSfzhm1.matcher(sfzhm);
Matcher matcherSfzhm2 = patternSfzhm2.matcher(sfzhm);
Pattern patternSjhm = Pattern
.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher matcherSjhm = patternSjhm.matcher(sjhm);
Pattern patternDzyx = Pattern
.compile("^([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})(\\] )$");
Matcher matcherDzyx = patternDzyx.matcher(dzyx);
if (xm.equals("")) {
showToast("姓名不能为空!");
}
else if (dlzh.equals("")) {
showToast("登录账号不能为空!");
}
else if (dlmm.equals("")) {
showToast("登录密码不能为空!");
}
else if (sfzhm.equals("")) {
showToast("身份证号码不能为空!");
}
else if (!(sfzhm.equals("")) && !matcherSfzhm1.find() && !matcherSfzhm2.find()) {
showToast("身份证号码格式不正确,请重新输入!");
}
else if(!(sjhm.equals("")) && !(matcherSjhm.find())) {
showToast("手机号码格式不正确,请重新输入!");
}
else if(!(dzyx.equals("")) && !((matcherDzyx.find()))) {
showToast("电子邮箱格式不正确,请重新输入!");
}


JS中:


[java script]
if(/^$/.test(xm)) {
alert("姓名不能为空!");