Email and phone Number Validation in android

后端 未结 12 1286
青春惊慌失措
青春惊慌失措 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:00

    For Email Address Validation

    private boolean isValidMail(String email) {
    
        String EMAIL_STRING = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    
        return Pattern.compile(EMAIL_STRING).matcher(email).matches();
    
    }
    

    OR

    private boolean isValidMail(String email) {
       return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }
    

    For Mobile Validation

    For Valid Mobile You need to consider 7 digit to 13 digit because some country have 7 digit mobile number. If your main target is your own country then you can match with the length. Assuming India has 10 digit mobile number. Also we can not check like mobile number must starts with 9 or 8 or anything.

    For mobile number I used this two Function:

    private boolean isValidMobile(String phone) {
        if(!Pattern.matches("[a-zA-Z]+", phone)) {
            return phone.length() > 6 && phone.length() <= 13;
        }
        return false;
    }
    

    OR

    private boolean isValidMobile(String phone) {
        return android.util.Patterns.PHONE.matcher(phone).matches();    
    }
    

提交回复
热议问题