Open FernandoX7 opened 7 years ago
Also considers partial domain names to be valid.
@NotEmpty
@Email
@Bind(R.id.emailInput)
EditText emailInput;
Validates the following:
emailname@gm
emailname@gmail
emailname@* (various combinations of letters **without full domain name with .com .net .org** etc)
Why is this, any quick solution without writing a custom rule?
Why is this? And is there a workaround
@lopezzbelgium @dieter115 I ended up completely removing this library from my project.
Just use Android's util pattern if what you're doing isn't too complex: https://developer.android.com/reference/android/util/Patterns.html#EMAIL_ADDRESS
This is valid for API 8 and above with Android Studio's suggestion here as an example:
public static boolean isEmailValid(String email) {
return !(email == null || TextUtils.isEmpty(email)) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
Hi guys,
I was also facing this issue so I went ahead and created my own email rule. PFB code snippets.
Email.java
import com.mobsandgeeks.saripaar.annotation.ValidateUsing;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@ValidateUsing(EmailRule.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Email {
int sequence() default -1;
int messageResId() default -1;
String message() default "Invalid email!";
}
EmailRule.java
import android.text.TextUtils;
import android.util.Patterns;
import com.mobsandgeeks.saripaar.AnnotationRule;
class EmailRule extends AnnotationRule<Email, String> {
protected EmailRule(Email email) {
super(email);
}
@Override
public boolean isValid(final String email) {
return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
}
And then use it as follow.
@Order(1)
@NotEmpty(sequence = 1, messageResId = R.string.empty_email_msg)
@Email(sequence = 2, messageResId = R.string.invalid_email_msg)
@BindView(R.id.et_email)
TextInputEditText etEmail;
And register the annotation to the validator (this part is important):
validator.registerAnnotation(Email.class);
Hope it helps.
Thanks @ganesh2shiv
Good solution @ganesh2shiv. There is to have into account that there is to import that Email.java
class intead of the old one. In fact I have renamed it to avoid confusions... 😄
Many thanks @ganesh2shiv..!!! You saved my day.
Just like the title says, when validating an email with
@NotEmpty @Email
and you use an email without the .com part, it considers it a valid email