How to validate mobile number using regular expression. mobile number must starts with non zero number and contains only 10 digits
Asked
Active
Viewed 1,277 times
-2
-
1Thank you for the exercise, prof. – Phantômaxx Feb 14 '15 at 17:49
-
2possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – i_saw_drones Feb 14 '15 at 17:50
2 Answers
1
Try something like:
System.out.println("1000000000".matches("^[1-9][0-9]{9}$"));
System.out.println("0000000030".matches("^[1-9][0-9]{9}$"));
System.out.println("000000003301".matches("^[1-9][0-9]{9}$"));
Output:
true
false
false

SMA
- 36,381
- 8
- 49
- 73
0
I don't see why you would need an RE(If you really do, read almas's answer), you mentioned java and android, a simple method like this should do (Assuming you have the number as a string "num"):
private boolean validateNumber(String num) {
if(num.length = 10 && !num[0].equals("0")){
return true;
} else {
return false;
}
}