how to Validate a Phone number so that it should not allow all same numerics like 99999999999
or 11111111111
in JAVA
如何验证电话号码,以便它不允许所有相同的数字,如JAVA中的99999999999或11111111111
thanks Sunny Mate
谢谢Sunny Mate
3 个解决方案
#1
9
The following regex:
以下正则表达式:
^(\d)(?!\1+$)\d{10}$
matches 11 digit strings that do not have all the same digits.
匹配没有全部相同数字的11位数字符串。
A demo:
一个演示:
public class Main {
public static void main(String[] args) throws Exception {
String[] tests = {
"11111111111",
"99999999999",
"99999999998",
"12345678900"
};
for(String t : tests) {
System.out.println(t+" :: "+t.matches("(\\d)(?!\\1+$)\\d{10}"));
}
}
}
which produces:
产生:
11111111111 :: false
99999999999 :: false
99999999998 :: true
12345678900 :: true
#2
15
If feasable, I'd try to discredit that requirement so it will be rejected.
如果可行,我会试图抹黑该要求,以免被拒绝。
No matter what you put into your plausibility checks, a user trying to avoid mandatory fields by entering junk into them will always succeed. You either end up having "smarter" harder-to-detect junk data items, or having a plausibility check which does not let all real-world data through into the system. Shit in, shit out. Build a shitshield, and your users will create fascies you never imagined.
无论你在合理性检查中加入什么,用户试图通过在其中输入垃圾来避免强制字段将始终成功。您要么最终拥有“更聪明”难以检测的垃圾数据项,要么进行合理性检查,不会让所有真实数据通过系统。屎,吐出来。构建一个shitshield,您的用户将创建您从未想象过的内容。
There is no way to program around that (except for simple things that usually are unintended, erraneously entered typos and so on).
没有办法围绕它编程(除了通常是无意识的简单事物,错误地输入错别字等)。
#3
5
This code matches numbers with at least 4 repeated numbers. (You could change the 3 in the regular expression to increase this threshold.)
此代码匹配至少包含4个重复数字的数字。 (您可以更改正则表达式中的3以增加此阈值。)
Pattern sameDigits = Pattern.compile("(\\d)(\\1){3,}");
for (String num : new String[] {
"11111", // matches
"1234", // does not match
"8584", // does not match
"999", // does not match (too few repetitions)
"9999"}) // matches
if (sameDigits.matcher(num).matches())
System.out.println("Same digits: " + num);
else
System.out.println("Not same digits: " + num);
Prints
打印
Same digits: 11111
Not same digits: 1234
Not same digits: 8584
Not same digits: 999
Same digits: 9999
#1
9
The following regex:
以下正则表达式:
^(\d)(?!\1+$)\d{10}$
matches 11 digit strings that do not have all the same digits.
匹配没有全部相同数字的11位数字符串。
A demo:
一个演示:
public class Main {
public static void main(String[] args) throws Exception {
String[] tests = {
"11111111111",
"99999999999",
"99999999998",
"12345678900"
};
for(String t : tests) {
System.out.println(t+" :: "+t.matches("(\\d)(?!\\1+$)\\d{10}"));
}
}
}
which produces:
产生:
11111111111 :: false
99999999999 :: false
99999999998 :: true
12345678900 :: true
#2
15
If feasable, I'd try to discredit that requirement so it will be rejected.
如果可行,我会试图抹黑该要求,以免被拒绝。
No matter what you put into your plausibility checks, a user trying to avoid mandatory fields by entering junk into them will always succeed. You either end up having "smarter" harder-to-detect junk data items, or having a plausibility check which does not let all real-world data through into the system. Shit in, shit out. Build a shitshield, and your users will create fascies you never imagined.
无论你在合理性检查中加入什么,用户试图通过在其中输入垃圾来避免强制字段将始终成功。您要么最终拥有“更聪明”难以检测的垃圾数据项,要么进行合理性检查,不会让所有真实数据通过系统。屎,吐出来。构建一个shitshield,您的用户将创建您从未想象过的内容。
There is no way to program around that (except for simple things that usually are unintended, erraneously entered typos and so on).
没有办法围绕它编程(除了通常是无意识的简单事物,错误地输入错别字等)。
#3
5
This code matches numbers with at least 4 repeated numbers. (You could change the 3 in the regular expression to increase this threshold.)
此代码匹配至少包含4个重复数字的数字。 (您可以更改正则表达式中的3以增加此阈值。)
Pattern sameDigits = Pattern.compile("(\\d)(\\1){3,}");
for (String num : new String[] {
"11111", // matches
"1234", // does not match
"8584", // does not match
"999", // does not match (too few repetitions)
"9999"}) // matches
if (sameDigits.matcher(num).matches())
System.out.println("Same digits: " + num);
else
System.out.println("Not same digits: " + num);
Prints
打印
Same digits: 11111
Not same digits: 1234
Not same digits: 8584
Not same digits: 999
Same digits: 9999