Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Validate a Phone number so that it should not allow all same numerics like 99999999999 or 11111111111 in java

Tags:

java

regex

how to Validate a Phone number so that it should not allow all same numerics like 99999999999 or 11111111111 in JAVA

thanks Sunny Mate

like image 264
Suresh Avatar asked Oct 21 '10 10:10

Suresh


People also ask

How do you validate a number in Java?

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.

How do I validate a 10 digit mobile number?

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.

How can I check mobile number in regex?

/^([+]\d{2})? \d{10}$/ This is how this regex for mobile number is working. + sign is used for world wide matching of number.


2 Answers

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).

like image 136
TheBlastOne Avatar answered Nov 15 '22 06:11

TheBlastOne


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
like image 22
Bart Kiers Avatar answered Nov 15 '22 08:11

Bart Kiers