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

该Java代码示例展示了如何生成6位和10位的随机验证码,以及如何验证一个字符串是否符合中国手机号码的格式。通过SecureRandom类实现随机数生成,并提供了正则表达式来匹配手机号码的有效性。
摘要由CSDN通过智能技术生成

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值