0
public static boolean isEmailValid(String email) {
        boolean isValid = false;

        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";

        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(email);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

I am calling this in Activity for email validation

  if (!Util.isEmailValid(binding.etEmail.getText().toString().trim())) {
                Toast.makeText(this, getResources().getString(R.string.valid_email), Toast.LENGTH_SHORT).show();
                return false;
            }

i am getting the crash report .

java.util.regex.PatternSyntaxException: Error in {min,max} interval near index 7 (+\d{٣}[\s-]?\d{0}[\s-]?)

let me know if not clear i will post full crash report

Please help me what i am doing wrong and how to fix this crash .

1 Answer 1

1

Use this

String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"

instead of this

String emailPattern = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for help me ,but will it cover for all validation which is given in question ?
Yup. Most of them. Why don't you test them out? Thank you.
Thanks its working but can you please help me to understand why this crash is coming this crash is not coming for all device its coming for specific device like Android OS-10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.