is Legal Name with English and Chinese character by regex - Java Internationalization

Java examples for Internationalization:Chinese

Description

is Legal Name with English and Chinese character by regex

Demo Code

import java.util.regex.Pattern;

public class Main {

  public static void main(String[] argv) {
    String name = "java2s.com";
    System.out.println(isLegalName(name));

    name = "#$%^&*";
    System.out.println(isLegalName(name));
  }/*from   www. j  a  va2 s. co m*/

  public final static Pattern LEGAL_NAME = Pattern.compile("^[a-zA-Z_0-9\\.`~!@#$%^&*()+-=/\\u4e00-\\u9fa5]{4,20}$");

  public static boolean isLegalName(String name) {
    return !isEmpty(name) && LEGAL_NAME.matcher(name).matches();
  }

  public static boolean isEmpty(String str) {
    return str == null || str.trim().length() == 0 || str.trim().equalsIgnoreCase("null");
  }

}

Related Tutorials