Java random password generation and email mobile phone number and match

  • 2020-05-05 11:11:36
  • OfStack

Nonsense not to say, directly to everyone posted java code, code has comments, write is not good, also please everyone a lot of attention.

The code looks like this:


package com.alibaba.uyuni.common.util;
import java.util.Random;
public class GeneratePassword {
/**
*  Generate random password 
* @param pwd_len
*  The total length of the generated password 
* @return  String of password 
*/
public static String genRandomNum(int pwd_len) {
// 26*2 A letter +10 A digital 
final int maxNum = 62;
int i; //  Generated random Numbers 
int count = 0; //  The length of the generated password 
char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while (count < pwd_len) {
//  Generate random Numbers, take the absolute value, prevent negative Numbers, 
i = Math.abs(r.nextInt(maxNum)); //  The number generated is the largest 62-1
if (i >= 0 && i < str.length) {
pwd.append(str[i]);
count++;
}
}
return pwd.toString();
}
public static void main(String[] args) {
System.out.println(genRandomNum(6));// 
}
}
package com.alibaba.uyuni.common.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexUtils {
/**
*  validation Email
* @param email email Address, format: zhangsan@zuidaima.com . zhangsan@xxx.com.cn . xxx Representative mail service provider 
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkEmail(String email) { 
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?"; 
return Pattern.matches(regex, email); 
} 
/**
*  validation *** number 
* @param idCard  residents *** number 15 or 18 The last digit may be a number or a letter 
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkIdCard(String idCard) { 
String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}"; 
return Pattern.matches(regex,idCard); 
} 
/**
*  Verify mobile phone number (supports international format, +86135xxxx... (mainland China), +00852137xxxx... (Hong Kong, China) 
* @param mobile  The number segment of the mobile, unicom and telecom operators 
*<p> Moving segment: 134(0-8) , 135 , 136 , 137 , 138 , 139 , 147 (intended for TD Card) 
* , 150 , 151 , 152 , 157 ( TD Dedicated), 158 , 159 , 187 (not enabled), 188 ( TD Special) </p>
*<p> Section of China unicom: 130 , 131 , 132 , 155 , 156 (for world wind), 185 (not enabled), 186 ( 3g ) </p>
*<p> Telecom segment: 133 , 153 , 180 (not enabled), 189</p>
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkMobile(String mobile) { 
String regex = "(\\+\\d+)?1[3458]\\d{9}$"; 
return Pattern.matches(regex,mobile); 
} 
/**
*  Verify the landline number 
* @param phone  Telephone number, format: country (region) telephone code  +  Area code (city code)  +  Telephone Numbers such as: +8602085588447
* <p><b> Country (region)   code   : </b> A standard country code identifying the country (region) in which the telephone number is located. It contains from  0  to  9  One or more digits, 
*  The number is followed by a space-separated country code. </p>
* <p><b> Area code (city code) : </b> This may contain one or more from  0  to  9  The number, region or city code placed in parentheses -- 
*  For countries that do not use a region or city code, the component is omitted. </p>
* <p><b> Telephone no. : </b> This includes from  0  to  9  One or more Numbers of  </p>
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkPhone(String phone) { 
String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$"; 
return Pattern.matches(regex, phone); 
} 
/**
*  Validate integers (positive and negative) 
* @param digit  One or more 0-9 Integer between 
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkDigit(String digit) { 
String regex = "\\-?[1-9]\\d+"; 
return Pattern.matches(regex,digit); 
} 
/**
*  Validate integers and floating point Numbers (plus and minus integers and plus and minus floating point Numbers) 
* @param decimals  One or more 0-9 A floating point number between, such as: 1.23 . 233.30
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkDecimals(String decimals) { 
String regex = "\\-?[1-9]\\d+(\\.\\d+)?"; 
return Pattern.matches(regex,decimals); 
} 
/**
*  Validate whitespace character 
* @param blankSpace  White space characters, including: Spaces, \t , \n , \r , \f , \x0B
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkBlankSpace(String blankSpace) { 
String regex = "\\s+"; 
return Pattern.matches(regex,blankSpace); 
} 
/**
*  Verify the Chinese 
* @param chinese  Chinese characters 
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkChinese(String chinese) { 
String regex = "^[\u4E00-\u9FA5]+$"; 
return Pattern.matches(regex,chinese); 
} 
/**
*  Verification date (year/month/day) 
* @param birthday  Date, format: 1992-09-03 Or, 1992.09.03
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkBirthday(String birthday) { 
String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}"; 
return Pattern.matches(regex,birthday); 
} 
/**
*  validation URL address 
* @param url  Format: http://blog.csdn.net:80/xyang81/article/details/7705960?  or  http://www.csdn.net:80
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkURL(String url) { 
String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?"; 
return Pattern.matches(regex, url); 
} 
/**
* <pre>
*  Access to the website  URL  First class domain name 
* http://www.zuidaima.com/share/1550463379442688.htm ->> zuidaima.com
* </pre>
* 
* @param url
* @return
*/
public static String getDomain(String url) {
Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
//  Get the full domain name 
// Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(url);
matcher.find();
return matcher.group();
}
/**
*  Match the Chinese postal code 
* @param postcode  The zip code 
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkPostcode(String postcode) { 
String regex = "[1-9]\\d{5}"; 
return Pattern.matches(regex, postcode); 
} 
/**
*  matching IP address ( Simple matching, format, such as: 192.168.1.1 . 127.0.0.1 , there is no match IP The size of the segment )
* @param ipAddress IPv4 Standard address 
* @return  Verify successful return true , the validation failure returns false
*/ 
public static boolean checkIpAddress(String ipAddress) { 
String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))"; 
return Pattern.matches(regex, ipAddress); 
} 
}

The above is the site to share Java random password generation and email, mobile phone match the relevant content, I hope to help you.


Related articles: