如何使用字符两个字节的电子邮件地址验证来输入日语?

时间:2022-06-24 22:40:38

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

如firstname-lastname@domain.com应该有效,但被视为无效。所有案例都是正确的https://blogs.msdn.microsoft.com/testing123/2009/02/06/email-address-test-cases/只有案例firstname-lastname@domain.com - 返回false

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

这是我的代码,我使用的是unicode Halfwidth和Fullwidth Forms http://jrgraphix.net/r/Unicode/FF00-FFEF

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

case hyphen-minus错误 - > FF0D返回false。预期结果:返回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!

如果有人可以提供帮助,我们将不胜感激!

3 个解决方案

#1


2  

firstname―lastname@domain.com should be valid

firstname-lastname@domain.com应该有效

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.

不是。此地址中没有@(U + 0040 Commercial At)符号,只有@(U + FF20 Fullwidth Commercial At),它不是有效的本地/部分/域分隔符。

(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:

通常,当接受来自日本用户的输入时,最好将所有“兼容性”全宽和半宽字符转换为普通的ASCII字符。使用Unicode Normalizer,然后您可以尝试验证字符串:

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.

请注意,此示例中的短划线字符既不是ASCII连字符也不是Fullwidth形式。这是一个U + 2015单杠。不知道那会是从哪里来的。虽然在国际化的电子邮件地址中使用本地部分在技术上是有效的,但许多电子邮件地址验证器会拒绝它,因为它很奇怪,可能不是那个意思。

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

不要花太多时间来验证电子邮件地址。你可以疯狂地尝试以正则表达式复制RFC的确切规则(即使是那些没有人使用但实际上不会工作的那些规则),或者你可能会疯狂地试图强制执行你自己的想法可能正确的地址。更好地避免疯狂并保持简单。例如它是否包含@然后是a。?然后让我们尝试发送它,看看会发生什么

#2


0  

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.

它已在abc_d@gmail.com上测试过。

#3


0  

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

防爆。 String str = edittextemail.gettext()。tostring();

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

#1


2  

firstname―lastname@domain.com should be valid

firstname-lastname@domain.com应该有效

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.

不是。此地址中没有@(U + 0040 Commercial At)符号,只有@(U + FF20 Fullwidth Commercial At),它不是有效的本地/部分/域分隔符。

(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:

通常,当接受来自日本用户的输入时,最好将所有“兼容性”全宽和半宽字符转换为普通的ASCII字符。使用Unicode Normalizer,然后您可以尝试验证字符串:

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.

请注意,此示例中的短划线字符既不是ASCII连字符也不是Fullwidth形式。这是一个U + 2015单杠。不知道那会是从哪里来的。虽然在国际化的电子邮件地址中使用本地部分在技术上是有效的,但许多电子邮件地址验证器会拒绝它,因为它很奇怪,可能不是那个意思。

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

不要花太多时间来验证电子邮件地址。你可以疯狂地尝试以正则表达式复制RFC的确切规则(即使是那些没有人使用但实际上不会工作的那些规则),或者你可能会疯狂地试图强制执行你自己的想法可能正确的地址。更好地避免疯狂并保持简单。例如它是否包含@然后是a。?然后让我们尝试发送它,看看会发生什么

#2


0  

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.

它已在abc_d@gmail.com上测试过。

#3


0  

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

防爆。 String str = edittextemail.gettext()。tostring();

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