Java Code Validation user name, support Chinese and English (including full-width characters), numbers, underscores and minus signs (full-width and kanji two-bit), 4-20-bit length, two-bit count

Source: Internet
Author: User

Package com.sangedabuliu.www; Import Java.util.regex.matcher;import Java.util.regex.Pattern; public class Userreg {/** * validates username, supports both Chinese and English (including full-width characters), numbers, underscores, and minus signs (full-width and kanji two-bit), 4-20-bit length, Chinese by two-bit count * @author Www.sangedabul iu.com * @param userName * @return * * public static Boolean validateusername (String userName) {Stri        ng validatestr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uff21-\uff3a\uff41-\uff5a]+$";        Boolean rs = false;        rs = Matcher (Validatestr, userName);            if (RS) {int strlenth = getstrlength (userName);            if (Strlenth < 4 | | Strlenth >) {rs = false;    }} return RS; }/** * Gets the length of the string, the double character (including kanji) is counted by two bits * * @param value * @return */public static int Getstrlength (        String value) {int valuelength = 0;        String Chinese = "[\u0391-\uffe5]";          for (int i = 0; i < value.length (); i++) {String temp = value.substring (i, i + 1);  if (Temp.matches (Chinese)) {valuelength + = 2;            } else {valuelength + = 1;    }} return valuelength;        } private static Boolean Matcher (String Reg, String string) {Boolean tem = FALSE;        Pattern pattern = Pattern.compile (reg);        Matcher Matcher = Pattern.matcher (string);        TEM = matcher.matches ();    return tem;        } public static void Main (string[] args) {String str = "0-_f9zd 22"; String st = "a-dq_!!!! Go to symbol Labels! ノチセたのひちぬ! Of course.!!..         * * Half angle ";        SYSTEM.OUT.PRINTLN (Validateusername (str));        System.out.println (St.replaceall ("[\\pp&&[^-_]]", ""));    System.out.println (St.replaceall ("[\\w\\--Calls A-za-z]", "")); }}

Java Code Validation user name, support Chinese and English (including full-width characters), numbers, underscores and minus signs (full-width and kanji two-bit), 4-20-bit length, two-bit count

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.