validating email address which contains non-english (UTF-8) character in Java

隐身守侯 提交于 2019-12-12 05:48:57

问题


i am having fallowing email id

闪闪发光@闪闪发光.com 

i need to validate this type of email at server side so that user can not enter this type of email..
i have solved similar problem in javascript by using below regex-

/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/gi

But. unable to do same thing in java.Please help me guys.
Thanks in advance!!!


回答1:


The Java regular expression pattern (?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6} should suffice. Here's what the pattern means:

(?i)            # Case insensitive flag
[-a-z0-9+_]     # First character
[-a-z0-9+_.]*   # Zero or more characters
@               # Literal '@' character
[-a-z0-9]       # Match a single character
[-a-z0-9.]*     # Match zero or more characters
\.              # Literal '.' character
[a-z]{2,6}      # Match 2 through 6 alpha characters

The following test code ...

final String ps =
        "(?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6}";
final Pattern p = Pattern.compile(ps);
for (String s : new String[] {"foo@bar.COM", "+foo@bar.COM",
        "-foo@bar.COM", "fo_o@bar.COM", "f.oo@bar.COM", "a@b.cdefgh",
        "3@4.com", "3@4.5.6-7.8.com", ".foo@bar.com", "a@b.cdefghi",
        "闪闪发光@闪闪发光.com"})
{
    final Matcher m = p.matcher(s);
    if (m.matches()) {
        System.out.println("Success: " + s);
    } else {
        System.out.println("Fail: " + s);
    }
}

... will output:

Success: foo@bar.COM
Success: +foo@bar.COM
Success: -foo@bar.COM
Success: fo_o@bar.COM
Success: f.oo@bar.COM
Success: a@b.cdefgh
Success: 3@4.com
Success: 3@4.5.6-7.8.com
Fail: .foo@bar.com
Fail: a@b.cdefghi
Fail: 闪闪发光@闪闪发光.com

By using the Matcher.matches() method, you don't need to include the ^ start-of-line or $ end-of-line boundary matching constructs since Matcher.matches() will match on the whole string.




回答2:


[Update] Sorry for the js code. Try this:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


    public class EmailValidator{

          private Pattern pattern;
          private Matcher matcher;

          private static final String EMAIL_PATTERN = 
                       "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@
                       [A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

          public EmailValidator(){
              pattern = Pattern.compile(EMAIL_PATTERN);
          }

          /**
           * Validate hex with regular expression
           * @param hex hex for validation
           * @return true valid hex, false invalid hex
           */
          public boolean validate(final String hex){

              matcher = pattern.matcher(hex);
              return matcher.matches();

          }
    }


来源:https://stackoverflow.com/questions/7554827/validating-email-address-which-contains-non-english-utf-8-character-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!