Reputation: 16758
I have an EditText defined in layout xml like this:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/enter_phone_field"
android:layout_below="@id/enter_name_field"
android:layout_alignRight="@id/destination_value_label"
android:layout_alignEnd="@id/destination_value_label" />
I have implemented its OnFocusChangeListener like this:
enterPhoneNumber = (EditText)findViewById(R.id.enter_phone_field);
// phone number lost focus listener
enterPhoneNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
if (PhoneNumberUtils.isGlobalPhoneNumber(enterPhoneNumber.getText().toString())){
//valid phone number detected
Log.i("PhoneNumberValidated", enterPhoneNumber.getText().toString());
}
else {
enterPhoneNumber.setError("Please enter valid phone number");
}
}
}
});
I tried entering 34
in enterPhoneNumber field, and when I tapped out of it for some reasons it did not show error on it.
Any ideas?
Upvotes: 0
Views: 866
Reputation: 5621
Because "34" is global number by definition of the PhoneNumberUtils.isGlobalPhoneNumber which in its turn checks that "34" matches the pattern:
private static final Pattern GLOBAL_PHONE_NUMBER_PATTERN =
Pattern.compile("[\\+]?[0-9.-]+");
Upvotes: 0