Email and phone Number Validation in android

后端 未结 12 1265
青春惊慌失措
青春惊慌失措 2020-12-02 11:03

I have a registration form in my application which I am trying to validate. I\'m facing some problems with my validation while validating the phone number and email fields.<

12条回答
  •  旧巷少年郎
    2020-12-02 12:05

    Android has build-in patterns for email, phone number, etc, that you can use if you are building for Android API level 8 and above.

    private boolean isValidEmail(CharSequence email) {
        if (!TextUtils.isEmpty(email)) {
            return Patterns.EMAIL_ADDRESS.matcher(email).matches();
        }
        return false;
    }
    
    private boolean isValidPhoneNumber(CharSequence phoneNumber) {
        if (!TextUtils.isEmpty(phoneNumber)) {
            return Patterns.PHONE.matcher(phoneNumber).matches();
        }
        return false;
    }
    

提交回复
热议问题