Email and phone Number Validation in android

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

    //validation class

    public class EditTextValidation {

    public static boolean isValidText(CharSequence target) {
        return target != null && target.length() != 0;
    }
    
    public static boolean isValidEmail(CharSequence target) {
        if (target == null) {
            return false;
        } else {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
        }
    }
    
    public static boolean isValidPhoneNumber(CharSequence target) {
        if (target.length() != 10) {
            return false;
        } else {
            return android.util.Patterns.PHONE.matcher(target).matches();
        }
    }
    

    //activity or fragment

        val userName = registerNameET.text?.trim().toString()
        val mobileNo = registerMobileET.text?.trim().toString()
        val emailID = registerEmailIDET.text?.trim().toString()
    
        when {
            !EditTextValidation.isValidText(userName) -> registerNameET.error = "Please provide name"
            !EditTextValidation.isValidEmail(emailID) -> registerEmailIDET.error =
                "Please provide email"
            !EditTextValidation.isValidPhoneNumber(mobileNo) -> registerMobileET.error =
                "Please provide mobile number"
            else -> {
                showToast("Hello World")
            }
        }
    

    **Hope it will work for you... It is a working example.

    0 讨论(0)
  • 2020-12-02 11:51
    public boolean checkForEmail() {
            Context c;
            EditText mEtEmail=(EditText)findViewById(R.id.etEmail);
            String mStrEmail = mEtEmail.getText().toString();
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(mStrEmail).matches()) {
                return true;
            }
            Toast.makeText(this,"Email is not valid", Toast.LENGTH_LONG).show();
            return false;
        }
    
    
        public boolean checkForMobile() {
            Context c;
            EditText mEtMobile=(EditText)findViewById(R.id.etMobile);
            String mStrMobile = mEtMobile.getText().toString();
            if (android.util.Patterns.PHONE.matcher(mStrMobile).matches()) {
                return true;
            }
            Toast.makeText(this,"Phone No is not valid", Toast.LENGTH_LONG).show();
            return false;
        }
    
    0 讨论(0)
  • 2020-12-02 11:55

    try this:

    extMobileNo.addTextChangedListener(new MyTextWatcher(extMobileNo));
    
    private boolean validateMobile()    {   
    
        String mobile =extMobileNo.getText().toString().trim();
        if(mobile.isEmpty()||!isValidMobile(mobile)||extMobileNo.getText().toString().toString().length()<10 || mobile.length()>13 )
    
        {
                inputLayoutMobile.setError(getString(R.string.err_msg_mobile));
            requestFocus(extMobileNo);
            return false;
        }
    
        else {
            inputLayoutMobile.setErrorEnabled(false);
        }
    
        return true;
    }
    
    private static boolean isValidMobile(String mobile)
    {
        return !TextUtils.isEmpty(mobile)&& Patterns.PHONE.matcher(mobile).matches();
    }
    
    0 讨论(0)
  • 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();    
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-02 12:05

    in Kotlin you can use Extension function to validate input

    // for Email validation

     fun String.isValidEmail(): Boolean =
            this.isNotEmpty() && Patterns.EMAIL_ADDRESS.matcher(this).matches()
    

    // for Phone validation

    fun String.isValidMobile(phone: String): Boolean {
        return Patterns.PHONE.matcher(phone).matches()
    }
    
    0 讨论(0)
提交回复
热议问题