Java隨機(jī)密碼生成并和郵箱、手機(jī)號匹配
更新時(shí)間:2016年01月28日 10:20:41 作者:jethai
這篇文章主要介紹了Java隨機(jī)密碼生成并和郵箱、手機(jī)號匹配的相關(guān)資料,需要的朋友可以參考下
廢話不多說了,直接給大家貼java代碼了,代碼有所注釋,寫的不好,還請各位大家多多關(guān)照。
代碼如下所示:
package com.alibaba.uyuni.common.util;
import java.util.Random;
public class GeneratePassword {
/**
* 生成隨機(jī)密碼
* @param pwd_len
* 生成的密碼的總長度
* @return 密碼的字符串
*/
public static String genRandomNum(int pwd_len) {
// 26*2個字母+10個數(shù)字
final int maxNum = 62;
int i; // 生成的隨機(jī)數(shù)
int count = 0; // 生成的密碼的長度
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) {
// 生成隨機(jī)數(shù),取絕對值,防止生成負(fù)數(shù),
i = Math.abs(r.nextInt(maxNum)); // 生成的數(shù)最大為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 {
/**
* 驗(yàn)證Email
* @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表郵件服務(wù)商
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkEmail(String email) {
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
return Pattern.matches(regex, email);
}
/**
* 驗(yàn)證***號碼
* @param idCard 居民***號碼15位或18位,最后一位可能是數(shù)字或字母
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkIdCard(String idCard) {
String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
return Pattern.matches(regex,idCard);
}
/**
* 驗(yàn)證手機(jī)號碼(支持國際格式,+86135xxxx...(中國內(nèi)地),+00852137xxxx...(中國香港))
* @param mobile 移動、聯(lián)通、電信運(yùn)營商的號碼段
*<p>移動的號段:134(0-8)、135、136、137、138、139、147(預(yù)計(jì)用于TD上網(wǎng)卡)
*、150、151、152、157(TD專用)、158、159、187(未啟用)、188(TD專用)</p>
*<p>聯(lián)通的號段:130、131、132、155、156(世界風(fēng)專用)、185(未啟用)、186(3g)</p>
*<p>電信的號段:133、153、180(未啟用)、189</p>
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkMobile(String mobile) {
String regex = "(\\+\\d+)?1[3458]\\d{9}$";
return Pattern.matches(regex,mobile);
}
/**
* 驗(yàn)證固定電話號碼
* @param phone 電話號碼,格式:國家(地區(qū))電話代碼 + 區(qū)號(城市代碼) + 電話號碼,如:+8602085588447
* <p><b>國家(地區(qū)) 代碼 :</b>標(biāo)識電話號碼的國家(地區(qū))的標(biāo)準(zhǔn)國家(地區(qū))代碼。它包含從 0 到 9 的一位或多位數(shù)字,
* 數(shù)字之后是空格分隔的國家(地區(qū))代碼。</p>
* <p><b>區(qū)號(城市代碼):</b>這可能包含一個或多個從 0 到 9 的數(shù)字,地區(qū)或城市代碼放在圓括號——
* 對不使用地區(qū)或城市代碼的國家(地區(qū)),則省略該組件。</p>
* <p><b>電話號碼:</b>這包含從 0 到 9 的一個或多個數(shù)字 </p>
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkPhone(String phone) {
String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
return Pattern.matches(regex, phone);
}
/**
* 驗(yàn)證整數(shù)(正整數(shù)和負(fù)整數(shù))
* @param digit 一位或多位0-9之間的整數(shù)
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkDigit(String digit) {
String regex = "\\-?[1-9]\\d+";
return Pattern.matches(regex,digit);
}
/**
* 驗(yàn)證整數(shù)和浮點(diǎn)數(shù)(正負(fù)整數(shù)和正負(fù)浮點(diǎn)數(shù))
* @param decimals 一位或多位0-9之間的浮點(diǎn)數(shù),如:1.23,233.30
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkDecimals(String decimals) {
String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
return Pattern.matches(regex,decimals);
}
/**
* 驗(yàn)證空白字符
* @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkBlankSpace(String blankSpace) {
String regex = "\\s+";
return Pattern.matches(regex,blankSpace);
}
/**
* 驗(yàn)證中文
* @param chinese 中文字符
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkChinese(String chinese) {
String regex = "^[\u4E00-\u9FA5]+$";
return Pattern.matches(regex,chinese);
}
/**
* 驗(yàn)證日期(年月日)
* @param birthday 日期,格式:1992-09-03,或1992.09.03
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkBirthday(String birthday) {
String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
return Pattern.matches(regex,birthday);
}
/**
* 驗(yàn)證URL地址
* @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回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>
* 獲取網(wǎng)址 URL 的一級域名
* 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);
// 獲取完整的域名
// 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();
}
/**
* 匹配中國郵政編碼
* @param postcode 郵政編碼
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false
*/
public static boolean checkPostcode(String postcode) {
String regex = "[1-9]\\d{5}";
return Pattern.matches(regex, postcode);
}
/**
* 匹配IP地址(簡單匹配,格式,如:192.168.1.1,127.0.0.1,沒有匹配IP段的大小)
* @param ipAddress IPv4標(biāo)準(zhǔn)地址
* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回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);
}
}
以上所述是小編給大家分享的Java隨機(jī)密碼生成并和郵箱、手機(jī)匹配的相關(guān)內(nèi)容,希望對大家有所幫助。
相關(guān)文章
eclipse實(shí)現(xiàn)ECDSA數(shù)字簽名
這篇文章主要為大家詳細(xì)介紹了eclipse實(shí)現(xiàn)ECDSA數(shù)字簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Spring Security 和Apache Shiro你需要具備哪些條件
這篇文章主要介紹了Spring Security 和Apache Shiro你需要具備哪些條件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Eclipse安裝Aptana插件(注意對應(yīng)版本問題)
這篇文章主要為大家詳細(xì)介紹了Eclipse安裝Aptana插件的相關(guān)資料,特別注意對應(yīng)版本問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
java InterruptedException 異常中斷的實(shí)現(xiàn)
本文主要介紹了java InterruptedException 異常中斷的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
idea新建Springboot項(xiàng)目,設(shè)置默認(rèn)maven和jdk版本方式
這篇文章主要介紹了idea新建Springboot項(xiàng)目,設(shè)置默認(rèn)maven和jdk版本方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
java、springboot?接口導(dǎo)出txt方式
這篇文章主要介紹了java、springboot?接口導(dǎo)出txt方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

