Search code examples
javaregexvalidationemailphone-number

regex validation with android Java Phone , Email, Date


Can someone help me with these regex validation with android Java :
Date : yyyy-mm-dd
Phone : (0999)-999-99-99
Email : [email protected]


Solution

  • Since API Level 8 and above, Android has patterns for email, phone number e etc; that you can use. For example:

    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;
    }
    

    For more details, see: http://developer.android.com/reference/android/util/Patterns.html

    Hope this helps.