mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!280 Validator新增: 车架号(车辆识别码)验证、驾驶证(驾驶证档案编号)的正则校验
Merge pull request !280 from dazer007/v5-dev
This commit is contained in:
commit
64d95df15f
@ -127,6 +127,22 @@ public class PatternPool {
|
||||
* </pre>
|
||||
*/
|
||||
public static final Pattern CREDIT_CODE = Pattern.compile("^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$");
|
||||
/**
|
||||
* 车架号
|
||||
* 别名:车辆识别代号 车辆识别码
|
||||
* eg:LDC613P23A1305189
|
||||
* eg:LSJA24U62JG269225
|
||||
* 十七位码、车架号
|
||||
* 车辆的唯一标示
|
||||
*/
|
||||
public static final Pattern CAR_VIN = Pattern.compile("^[A-Za-z0-9]{17}$");
|
||||
/**
|
||||
* 驾驶证 别名:驾驶证档案编号、行驶证编号
|
||||
* eg:430101758218
|
||||
* 12位数字字符串
|
||||
* 仅限:中国驾驶证档案编号
|
||||
*/
|
||||
public static final Pattern CAR_DRIVING_LICENCE = Pattern.compile("^[0-9]{12}$");
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
|
@ -90,6 +90,14 @@ public class Validator {
|
||||
* 中国车牌号码
|
||||
*/
|
||||
public final static Pattern PLATE_NUMBER = PatternPool.PLATE_NUMBER;
|
||||
/**
|
||||
* 车架号;别名:车辆识别代号 车辆识别码;十七位码
|
||||
*/
|
||||
public final static Pattern CAR_VIN = PatternPool.CAR_VIN;
|
||||
/**
|
||||
* 驾驶证 别名:驾驶证档案编号、行驶证编号;12位数字字符串;仅限:中国驾驶证档案编号
|
||||
*/
|
||||
public final static Pattern CAR_DRIVING_LICENCE = PatternPool.CAR_DRIVING_LICENCE;
|
||||
|
||||
/**
|
||||
* 给定值是否为{@code true}
|
||||
@ -1136,4 +1144,61 @@ public class Validator {
|
||||
public static boolean isCreditCode(CharSequence creditCode) {
|
||||
return CreditCodeUtil.isCreditCode(creditCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否为车架号;别名:行驶证编号 车辆识别代号 车辆识别码
|
||||
*
|
||||
* @param value 值,17位车架号;形如:LSJA24U62JG269225、LDC613P23A1305189
|
||||
* @return 是否为车架号
|
||||
* @since 5.6.3
|
||||
*/
|
||||
public static boolean isCarVin(CharSequence value) {
|
||||
return isMatchRegex(CAR_VIN, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否为车架号;别名:行驶证编号 车辆识别代号 车辆识别码
|
||||
*
|
||||
* @param <T> 字符串类型
|
||||
* @param value 值
|
||||
* @param errorMsg 验证错误的信息
|
||||
* @return 验证后的值
|
||||
* @throws ValidateException 验证异常
|
||||
* @since 5.6.3
|
||||
*/
|
||||
public static <T extends CharSequence> T validateCarVin(T value, String errorMsg) throws ValidateException {
|
||||
if (!isCarVin(value)) {
|
||||
throw new ValidateException(errorMsg);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否为驾驶证 别名:驾驶证档案编号、行驶证编号
|
||||
* 仅限:中国驾驶证档案编号
|
||||
*
|
||||
* @param value 值,12位数字字符串,eg:430101758218
|
||||
* @return 是否为档案编号
|
||||
* @since 5.6.3
|
||||
*/
|
||||
public static boolean isCarDrivingLicence(CharSequence value) {
|
||||
return isMatchRegex(CAR_DRIVING_LICENCE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否为驾驶证 别名:驾驶证档案编号、行驶证编号
|
||||
*
|
||||
* @param <T> 字符串类型
|
||||
* @param value 值
|
||||
* @param errorMsg 验证错误的信息
|
||||
* @return 验证后的值
|
||||
* @throws ValidateException 验证异常
|
||||
* @since 5.6.3
|
||||
*/
|
||||
public static <T extends CharSequence> T validateCarDrivingLicence(T value, String errorMsg) throws ValidateException {
|
||||
if (!isCarDrivingLicence(value)) {
|
||||
throw new ValidateException(errorMsg);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@ -195,4 +195,15 @@ public class ValidatorTest {
|
||||
Assert.assertTrue(Validator.isBetween(0.19f, 0.1f, 0.2f));
|
||||
Assert.assertTrue(Validator.isBetween(0.19, 0.1, 0.2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCarVinTest(){
|
||||
Assert.assertTrue(Validator.isCarVin("LSJA24U62JG269225"));
|
||||
Assert.assertTrue(Validator.isCarVin("LDC613P23A1305189"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCarDrivingLicenceTest(){
|
||||
Assert.assertTrue(Validator.isCarDrivingLicence("430101758218"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user