Reputation: 69
I am currently making a registration for an application in android. I want to trap invalid mobile numbers entered in the EditText box. Once I click the submit button, the app will check if the mobile number entered is valid.
All valid number should start with "639"
. My question is how am I going to read the first three digits that the user entered? For example, the user enters 639158716271
, this a valid number. While 197837281919
is invalid.
Can anyone suggest how to accomplish this?
Thanks in advance!
Upvotes: 0
Views: 23830
Reputation: 23
Checking for valid mobile number in androidstrong text Add in your XML Edittext
android:maxLines="1"
android:maxLength="10"
android:inputType="number"
Upvotes: 0
Reputation: 916
You can use the startsWith
method in your string object:
String num = "639158716271"
if (num.startsWith("639") && num.length() == 12)
// valid
else
// invaid
Upvotes: 0
Reputation: 14709
//Get the string from the edit text by:
String number = yourEditText.getText().toString();
if(number != null && number.matches("639[0-9]{9}"))
//do what you need to do for valid input
else
//do what you need to do for invalid input
matches()
ensures that the entire string cooresponds (exactly) to the regular expression that it takes. 639[0-9]{9}
says that the string must start off with 639 and then be followed by exactly 9 digits (0-9). If you wanted to match "639"
followed by 7 to 9 numbers, for example, you would use: 639[0-9]{7,9}
. Regular expressions: http://docs.oracle.com/javase/tutorial/essential/regex/
Upvotes: 5
Reputation: 26017
Method 1:
Get an instance of the EditText:
EditText myEdit = (EditText) findViewById(R.id.edittext1);
Then get the string that is currently being displayed:
String phoneNumber = myEdit.getText().toString();
If its only initial number that you want to match, then you can just compare as follows:
String initialPart = phoneNumber.substring(0, 4);
//Get 1st three characters and then compare it with 639
boolean valid = initialPart.equals("639");
Then you can continue making other comparisons. However this method is prone to some mistakes and you might miss some corner case. So I suggest to go for method 2:
Method:2
However one another very good way is to use Google's libphonenumber library. The documentation says:
It is for parsing, formatting, storing and validating international phone numbers. The Java version is optimized for running on smartphone.
I have used it for verifying phone numbers. It is very easy to use and you don't need to take care of the corner cases. It takes into account your country/location and all sorts of formats that the user may enter. It checks if the number is valid for that region. It also takes care of all possible valid format that user may enter like:
"+xx(xx)xxxx-xxxx", "+x.xxx.xxx.xxx","+1(111)235-READ" ,"+1/234/567/8901", "+1-234-567-8901 x1234"
( here x is number).
Here is a sample usage of how to validate it:
PhoneNumber NumberProto = null;
String NumberStr = "639124463869"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
NumberProto = phoneUtil.parse(NumberStr, "CH");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
boolean isValid = phoneUtil.isValidNumber(NumberProto); // returns true or false
P.S: "CH"
is the country code for Switzerland. You can enter your country code based on your need. They are given here. Hope it helps.
Upvotes: 5
Reputation: 6058
Edit: This is a longer solution. Regex is the way to go for shorter code if all you really need is to verify the number as Steve pointed out.
===
First, restrict your EditText to accept only numbers, then you can use the following to verify the validity of the number.
public static final int NUMBER_LENGTH = 9; // This will be used for validation
/**
* Returns the prefix of a phone number
*/
public String getNumberPrefix(String number) {
if (number != null) {
if (number.length() > NUMBER_LENGTH) {
return number.substring(0, number.length() - NUMBER_LENGTH);
}
}
return "";
}
/**
* Checks if a phone number is valid based on length and prefix
*/
public boolean isValidNumber(String number, String prefix) {
if (prefix == null) {
prefix = "";
}
if (number != null) {
if (number.length() > 0) {
if ((number.length() == NUMBER_LENGTH + prefix.length()) && (getNumberPrefix(number).equals(prefix))) {
return true;
}
}
}
return false;
}
So in your case, you could set a constant number length:
Some examples to use the method:
isValidNumber("639158716271", "639"); // This will return true
isValidNumber("631111111111", "639"); // This will return false
isValidNumber("6391587", "639"); // This will return false
isValidNumber("123456789000", "639"); // This will return false
Upvotes: 0
Reputation: 419
<EditText
android:id="@+id/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="12"
/>
int number = Intiger.ParseInt(EditText.getText.toString());
if(number.startsWith("639")&& number.length == 12)
{
//valid
}
else
{
//invalid
}
Upvotes: 0