Java code uses regular validation and common tool methods

Source: Internet
Author: User

1. Regular verification Mailbox

  public static Boolean checkemail (String email) {       Boolean flag = false;       try{        String check = "^ ([a-z0-9a-z]+[-|\\.]?) +[a-z0-9a-z]@ ([a-z0-9a-z]+ (-[a-z0-9a-z]+) \ \.) +[a-za-z]{2,}$ ";        Pattern regex = pattern.compile (check);        Matcher Matcher = regex.matcher (email);        Flag = Matcher.matches ();       } catch (Exception e) {        flag = false;       }       return flag;      }  

2. Verify that the nickname is legal

   /** * Verify 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, English by two-bit count * @return */public static Boolean Validateusername (String userName) {string validatestr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uff21-\uff3a\uff41-\uf          F5a]+$ ";          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, counting the two characters (including Chinese characters) by two bits * @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;   }

3. Determine if it is a number

   public static  boolean isinteger (String value) {       Pattern p = pattern.compile ("^[0-9]{11}$");     Matcher m = p.matcher (value);     Boolean IsOK = M.find ();     return IsOK; }

4. Create a random number

public static int Buildrandom (int length) {int num = 1;double random = Math.random (); if (Random < 0.1) {random = random + 0.1;} for (int i = 0; i < length; i++) {num = num * 10;} return (int) ((random * num));

5. Get the model, browser type

  public static  String Gettypeas (httpservletrequest request) {   string typeas= request.getheader ("User-agent" ); if (Typeas.equals ("") | | Typeas==null) {typeas= "nothing";} return typeas; }

5. Get the IP address

  public static  string GetIP (HttpServletRequest request) {   string ip = Request.getheader ("x-forwarded-for");        if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {            IP = request.getheader ("Proxy-client-ip");        }        if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {            IP = request.getheader ("Wl-proxy-client-ip");        }        if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {            IP = request.getremoteaddr ();        }        if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {ip= "Unknown";}        return IP; }

6. Determine if the number is within the array

public static Boolean isIn (Integer substring, integer[] source) {if (Source = = NULL | | source.length = = 0) {return false;} for (int i = 0; i < source.length; i++) {Integer Asource = source[i];if (asource.equals (substring)) {return true;}} return false;}

Java code uses regular validation and common tool methods

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.