In today's Internet applications, the phone number has been completely replaced e-mail has become uniquely identifies the user's registered account. With the rapid development of mobile networks, various mobile phone number prefix has introduced, how to verify a valid mobile number is the programmer must focus on function points, you say important, right? Did not seem so important, you say important, right? Once the program error, the user can not be registered and that is a major event!
So how do you see the following passage verify the phone number is correct code?
public String validatePhone(String phoneStr) {
if (phoneStr != null) {
if (phoneStr.length() == 11) {
if (isNumeric(phoneStr)) {
if (phoneStr.startsWith("130") || phoneStr.startsWith("131") || phoneStr.startsWith("132")
|| phoneStr.startsWith("134") || phoneStr.startsWith("135") || phoneStr.startsWith("136")
|| phoneStr.startsWith("137") || phoneStr.startsWith("138") || phoneStr.startsWith("139")
|| phoneStr.startsWith("140") || phoneStr.startsWith("141") || phoneStr.startsWith("142")
|| phoneStr.startsWith("144") || phoneStr.startsWith("145") || phoneStr.startsWith("146")
|| phoneStr.startsWith("147") || phoneStr.startsWith("148") || phoneStr.startsWith("149")
|| phoneStr.startsWith("150") || phoneStr.startsWith("151") || phoneStr.startsWith("152")
|| phoneStr.startsWith("154") || phoneStr.startsWith("155") || phoneStr.startsWith("156")
|| phoneStr.startsWith("157") || phoneStr.startsWith("158") || phoneStr.startsWith("159")
|| phoneStr.startsWith("170") || phoneStr.startsWith("171") || phoneStr.startsWith("172")
|| phoneStr.startsWith("174") || phoneStr.startsWith("175") || phoneStr.startsWith("176")
|| phoneStr.startsWith("177") || phoneStr.startsWith("178") || phoneStr.startsWith("179")
|| phoneStr.startsWith("180") || phoneStr.startsWith("181") || phoneStr.startsWith("182")
|| phoneStr.startsWith("184") || phoneStr.startsWith("185") || phoneStr.startsWith("186")
|| phoneStr.startsWith("187") || phoneStr.startsWith("188") || phoneStr.startsWith("189")) {
return "手机号正确";
} else {
return "手机号规则错误";
}
} else {
return "手机号必须为数字";
}
} else {
return "手机号长度必须为11位";
}
} else {
return "手机号不能为空";
}
}
Logic does not seem a problem, but really good long-winded ah! ! !
So you have a better way to do this?
Please move the following link to comment, to collect their prizes:
https://gitee.com/oschina/bullshit-codes/blob/master/java/PhoneRuleValidate.java
6 anniversary of the code cloud, We are seeking a variety of pit father of code, a lot of prizes waiting for you to come and collect
See detailed participatory approach https://gitee.com/oschina/bullshit-codes
------ Dividing line------
Other pit father Tucao Code:
- Pit father Codes | StringBuffer use such methods What pit?
- Pit father Codes | How long have you written the longest line of code? ? ?
- Pit father Codes | + conditional loop, nested layers up to you?
- Pit father Codes | late to optimize the query speed - quite business-minded!
- Pit father Codes | how you play then becomes abnormally play unusual?
- Pit father Codes | Stream play the most Code 6, seen people are shocked
- Pit father Codes | defer Go language can create a number of pits to?
- Pit father of code | log this package in the end it is still useful pit father?