android-saripaar icon indicating copy to clipboard operation
android-saripaar copied to clipboard

This is considered a valid email 'john@gmail' without the .com

Open FernandoX7 opened this issue 7 years ago • 8 comments

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

FernandoX7 avatar Jan 23 '17 04:01 FernandoX7

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)

Cherubyx avatar Feb 23 '17 15:02 Cherubyx

Why is this, any quick solution without writing a custom rule?

lopezzbelgium avatar Mar 29 '17 10:03 lopezzbelgium

Why is this? And is there a workaround

dieter115 avatar Mar 29 '17 11:03 dieter115

@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(); 
}

Cherubyx avatar Mar 29 '17 11:03 Cherubyx

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.

ganesh2shiv avatar Apr 11 '17 16:04 ganesh2shiv

Thanks @ganesh2shiv

ngugijames avatar May 12 '17 12:05 ngugijames

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... 😄

gabivitcord avatar Nov 02 '17 11:11 gabivitcord

Many thanks @ganesh2shiv..!!! You saved my day.

Dhaval2404 avatar Feb 26 '18 04:02 Dhaval2404