In my application there is a field called Land Line number, I want to restrict the user not to enter all zeros like "0000000000" but he can enter like "0123467889"."0801234567","98001245780" Starting zero and iin any position zero is acceptable, but not all zeros
Asked
Active
Viewed 524 times
-4
-
Can you share any code with us? – Tim Biegeleisen Sep 21 '16 at 10:23
-
http://stackoverflow.com/a/15644461/1134197 – aycanadal Sep 21 '16 at 10:25
-
1You can write a regular expression to check if your specific pattern has been matched. There is Pattern class that provides the following method, boolean matches (String regex, CharSequence input) – Aneeb Khawar Sep 21 '16 at 10:26
-
public static final boolean isValPhoneNumberLand(CharSequence target) { String regex = "[0123456789]{1}\\d{9}"; Pattern pattern = Pattern.compile(regex); boolean matcher; matcher = pattern.matcher(target).matches(); return matcher; } – Nandu Sep 21 '16 at 11:18
-
The above method i am using to validate, but it is accepting all zeros – Nandu Sep 21 '16 at 11:20
-
your regex isn't correct. Check the answer. – Aneeb Khawar Sep 21 '16 at 13:15
-
can u suggest me a regexp which suits my requirement – Nandu Sep 22 '16 at 06:05
1 Answers
0
This should do it,
private boolean isNumberValid(String number){
return Pattern.matches("^[0-9][1-9]{9}$", number);
}

Aneeb Khawar
- 330
- 1
- 8
-
it is not allowing all zeros, its fine, but it is not allowing zero any place of 10 digit num, My requirement is, not to allow all zeros But it can accept a ph num like 0801234567 etc., – Nandu Sep 22 '16 at 05:04
-
I dont think a regex alone can fulfill this requirement then. You need something like this stackoverflow.com/a/15644461/1134197. – Aneeb Khawar Sep 22 '16 at 05:11
-
Try this as well, android.util.Patterns.PHONE.matcher(number).matches(); – Aneeb Khawar Sep 22 '16 at 05:13