!280 Validator新增: 车架号(车辆识别码)验证、驾驶证(驾驶证档案编号)的正则校验

Merge pull request !280 from dazer007/v5-dev
This commit is contained in:
Looly 2021-03-19 11:46:14 +08:00 committed by Gitee
commit 64d95df15f
3 changed files with 92 additions and 0 deletions

View File

@ -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}$");
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**

View File

@ -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位车架号形如LSJA24U62JG269225LDC613P23A1305189
* @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;
}
}

View File

@ -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"));
}
}