What\'s a good technique for validating an e-mail address (e.g. from a user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn\'t seem to be
Simplest Kotlin solution using extension functions:
fun String.isEmailValid() =
Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
).matcher(this).matches()
and then you can validate like this:
"testemail6589@gmail.com".isEmailValid()
If you are in kotlin-multiplatform without access to Pattern
, this is the equivalent:
fun String.isValidEmail() = Regex(emailRegexStr).matches(this)
I have used follwing code.This works grate.I hope this will help you.
if (validMail(yourEmailString)){
//do your stuf
}else{
//email is not valid.
}
and use follwing method.This returns true if email is valid.
private boolean validMail(String yourEmailString) {
Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher emailMatcher = emailPattern.matcher(emailstring);
return emailMatcher.matches();
}
You can do any type of validation in android very easily by the oval.jar file. OVal is a pragmatic and extensible general purpose validation framework for any kind of Java objects.
follow this link: http://oval.sourceforge.net/userguide.html
You can downlaod this from here: http://oval.sourceforge.net/userguide.html#download
You can use validation by setting tags in variables
public class Something{
@NotEmpty //not empty validation
@Email //email validation
@SerializedName("emailAddress")
private String emailAddress;
}
private void checkValidation() {
Something forgotpass.setEmailAddress(LoginActivity.this.dialog_email.getText().toString());
Validator validator = new Validator();
//collect the constraint violations
List<ConstraintViolation> violations = validator.validate(forgotpass);
if(violations.size()>0){
for (ConstraintViolation cv : violations){
if(cv.getMessage().contains("emailAddress")){
dialog_email.setError(ValidationMessage.formattedError(cv.getMessage(), forgotpass));
}
}
}
}
Try this code.. Its really works..
if (!email
.matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"))
{
Toast.makeText(getApplicationContext(), "Email is invalid",
Toast.LENGTH_LONG).show();
return;
}
Note that most of the regular expressions are not valid for international domain names (IDN) and new top level domains like .mobi or .info (if you check for country codes or .org, .com, .gov and so on).
A valid check should separate the local part (before the at-sign) and the domain part. You should also consider the max length of the local part and domain (in sum 255 chars including the at-sign).
The best approach is to transform the address in an IDN compatible format (if required), validate the local part (RFC), check the length of the address and the check the availability of the domain (DNS MX lookup) or simply send an email.
email is your email-is.
public boolean validateEmail(String email) {
Pattern pattern;
Matcher matcher;
String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}