Email validation and Mobile number validation in Android

Email and Mobile numbers are very important parts of Android users. In this tutorial we will learn about how to Email validation and Mobile number validation in Android using regular expression.

Email Validation

public boolean isValidEmail(String email) {
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputEmail = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputEmail);
if (matcher.matches()) {
return true;
} else {
return false;
}
}

Mobile Number Validation for Indian numbers
public boolean isValidMobile(String mobile) {
String expression = "^(\\+91[\\-\\s]?)?[0]?(91)?[789]\\d{9}$";
CharSequence inputMobile = mobile;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputMobile);
if (matcher.matches()) {
return true;
} else {
return false;
}
}

Important Post:Button in Android

Source: Java Documentation