how to Validate a Phone number so that it should not allow all same numerics like 99999999999
or 11111111111
in JAVA
thanks Sunny Mate
Mobile number validation in Java is done using Pattern and Matcher classes of Java. The pattern class is used to compile the given pattern/regular expression and the matcher class is used to match the input string with compiled pattern/regular expression.
you can also use jquery for this length==10){ var validate = true; } else { alert('Please put 10 digit mobile number'); var validate = false; } } else { alert('Not a valid number'); var validate = false; } if(validate){ //number is equal to 10 digit or number is not string enter code here... } Save this answer.
/^([+]\d{2})? \d{10}$/ This is how this regex for mobile number is working. + sign is used for world wide matching of number.
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.
There is no way to program around that (except for simple things that usually are unintended, erraneously entered typos and so on).
The following regex:
^(\d)(?!\1+$)\d{10}$
matches 11 digit strings that do not have all the same digits.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With