简体   繁体   中英

How to email address validation with character two bytes for input from Japanese?

I'm trying to write a program where I ask the user to enter an email address. And then I verify if its a valid email address or not with two case input normal and input special characters two bytes

Like firstname―lastname@domain.com should be valid, but is considered invalid. All case is right https://blogs.msdn.microsoft.com/testing123/2009/02/06/email-address-test-cases/ Only case firstname―lastname@domain.com with return false

Here's my code, I'm using unicode Halfwidth and Fullwidth Forms http://jrgraphix.net/r/Unicode/FF00-FFEF

Error with case hyphen-minus -> FF0D return false. Expected result: return true

private VALIDATE_CODE validateInput(String username, String password) {

        if (username.length() == 0 || password.length() == 0) {
            return VALIDATE_CODE.EMPTY_USERNAME_PASSWORD;
        }

        if (!StringUtil.isValidEmail(username)) {
            return VALIDATE_CODE.INVALID_USERNAME_PASSWORD;
        }
        return VALIDATE_CODE.SUCCESS;
    }

 public static boolean isValidEmail(CharSequence email) {
    boolean validFullSize = ValidateUtil.EMAIL_ADDRESS_JAPANESE.matcher(email).matches();
    boolean validHaftSize = android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    return !TextUtils.isEmpty(email) && (validFullSize || validHaftSize);
}

public static final Pattern EMAIL_ADDRESS
    = 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}" +
        ")+"
    );


public static final Pattern EMAIL_ADDRESS_JAPANESE
        = Pattern.compile(
        "[\uFF41-\uFF5A\uFF21-\uFF3A\uFF10-\uFF19\\\uFF0B\\\uFF0E\\\uFF3F\\\uFF05\\\uFF0D\\\uFF0B]{1,256}" +
                "\\\uFF20" +
                "[\uFF41-\uFF5A\uFF21-\uFF3A\uFF10-\uFF19][\uFF41-\uFF5A\uFF21-\uFF3A\uFF10-\uFF19\\\uFF0D]{0,64}" +
                "(" +
                "\\\uFF0E" +
                "[\uFF41-\uFF5A\uFF21-\uFF3A\uFF10-\uFF19][\uFF41-\uFF5A\uFF21-\uFF3A\uFF10-\uFF19\\\uFF0D]{0,25}" +
                ")+"
);

If anyone could help, it would be greatly appreciated!

firstname―lastname@domain.com should be valid

It's not. There is no @ (U+0040 Commercial At) symbol in this address, only a (U+FF20 Fullwidth Commercial At) which is not a valid local-part/domain separator.

(There will also likely be delivery problems with the fullwidth chars in the local-part and domain; that may not technically make the address invalid in itself, but it's almost certainly mistaken input.)

In general when accepting input from Japanese users it's best to convert all the 'compatibility' fullwidth and halfwidth characters to normal ASCII ones. Use a Unicode Normalizer , then you have a string you can try to validate:

input = Normalizer.normalize(input, Normalizer.Form.NFKC);
"firstname―lastname@domain.com"

Note the dash character in this example is neither an ASCII hyphen nor a Fullwidth form. It's a U+2015 Horizontal Bar. No idea where that would have come from. Whilst it's technically valid to have in a local-part in an internationalized e-mail address, many e-mail address validators will reject it as it's pretty weird and probably not what was meant.

Don't spend too much time trying to validate e-mail addresses. You can go mad trying to replicate the exact rules of the RFCs in regex form (even the ones that no-one uses and which won't in reality work), or you can go mad trying to enforce your own ideas of what is a likely-correct address. Better avoid the madness and keep it simple. eg does it contain an @ and then a . ? then let's try to send to it and see what happens

Did you check your regular expressions if they are valid or not? I think the problem is in the Regular Expression.

Use this regular expression I hope it helps.

^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$

It has been tested on abc_d@gmail.com.

public final static boolean isValidEmail(CharSequence target) {
  return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

Ex. String str = edittextemail.gettext().tostring();

if(isValidEmail(str)){ // valid email is return true.
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM