您的位置:首页 > 编程语言 > Java开发

java生成随机六位数的验证码&随机生成十位数ValidCode码,用于邮件的验证&检查是不是符合为合法的中国的手机号码

2015-09-06 11:35 856 查看
package com.demo.test1;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HighArray {

private final static int OFFSET = 538309;

public static void main(String [] args) {

String str = MobileVfCode();
System.out.println(str+"请问");

System.out.println("----------------------------------------------");

System.out.println(generateMailValidCode()+"测试的时候用的???????");

System.out.println("=================================================");
boolean flag = isMobileNO("11111111111");
System.out.println(flag);

}

public static String MobileVfCode() {
long seed = System.currentTimeMillis() + OFFSET;
SecureRandom secureRandom = null; // 安全随机类
try {
secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(seed);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

String codeList = "1234567890"; // 验证码数字取值范围
String sRand = ""; // 定义一个验证码字符串变量

for (int i = 0; i < 6; i++) {
int code = secureRandom.nextInt(codeList.length() - 1); // 随即生成一个0-9之间的整数
String rand = codeList.substring(code, code + 1);
sRand += rand; // 将生成的随机数拼成一个六位数验证码
}
return sRand; // 返回一个六位随机数验证码

}

//随机生成十位数ValidCode码,用于邮件的验证
public static String generateMailValidCode() {
long seed = System.currentTimeMillis() + OFFSET;
SecureRandom secureRandom = null; // 安全随机类
try {
secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(seed);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

String codeList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabckefghijklmnopqrstuvwxyz1234567890"; // 验证码数字取值范围
String sRand = new String(""); // 定义一个验证码字符串变量

for (int i = 0; i < 10; i++) {
int code = secureRandom.nextInt(codeList.length() - 1); // 随即生成一个整数
String rand = codeList.substring(code, code + 1);
sRand += rand; // 将生成的随机数拼成一个十位数验证码
}
return sRand; // 返回一个六位随机数验证码
}

//检查是不是符合为合法的中国的手机号码
public static boolean isMobileNO(String mobiles) {
if (mobiles == null) {
return false;
}
Pattern p = Pattern
.compile("^((13[0-9])|(14[0-9])|(17[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: