Browse Source

车牌号码输入验证

master
wanghongjun 3 weeks ago
parent
commit
47f02e4675
  1. 51
      app/Services/ParkingLicensePlateService.php
  2. 6
      app/Services/ParkingSpaceService.php
  3. 4
      resources/lang/en/validation.php
  4. 4
      resources/lang/zh-CN/validation.php
  5. 4
      resources/lang/zh-TW/validation.php

51
app/Services/ParkingLicensePlateService.php

@ -193,4 +193,55 @@ class ParkingLicensePlateService
}
return $license_id;
}
// 验证车牌格式
public function validateLicensePlate($license_plate): string
{
$plate = str_replace(' ', '', $license_plate);
// 2. 根据车牌类型选择正则
if ($this->isMainlandChinaPlate($plate)) {
// 中国大陆车牌校验(禁止 I、O)
$pattern
= '/^[\x{4e00}-\x{9fa5}][A-Z](?:[A-HJ-NP-Z0-9]{5}|(?:[DF][A-HJ-NP-Z0-9][0-9]{4}|[0-9]{5}[DF]))$/u';
if (!preg_match($pattern, $plate)) {
return __validation('parking_space.zh_plate');
}
} elseif ($this->isHongKongPlate($plate)) {
// 中国香港车牌校验(禁止 I、O、Q)
$pattern = '/^[A-HJ-NP-Z0-9]{1,8}$/';
if (!preg_match($pattern, $plate)) {
return __validation('parking_space.hk_plate');
}
}
return '';
}
private function isMainlandChinaPlate(string $plate): bool
{
// 长度校验:普通车牌7位,新能源车牌8位[reference:12][reference:13]
$length = mb_strlen($plate);
if (!in_array($length, [7, 8])) {
return false;
}
// 基础格式校验:首字为汉字,第二位为大写字母
if (!preg_match('/^[\x{4e00}-\x{9fa5}][A-Z]/u', $plate)) {
return false;
}
return true;
}
private function isHongKongPlate(string $plate): bool
{
// 长度校验:1-8位
$length = strlen($plate);
if ($length < 1 || $length > 8) {
return false;
}
// 仅包含字母和数字
return ctype_alnum($plate);
}
}

6
app/Services/ParkingSpaceService.php

@ -341,6 +341,12 @@ class ParkingSpaceService extends BaseService
$licensePlateService = new ParkingLicensePlateService(
$this->logService
);
$error_str = $licensePlateService->validateLicensePlate(
$license_plate
);
if ($error_str) {
throw new CustomException($error_str);
}
$number_id = $licensePlateService->saveNumber(
$license_plate,
$oldValues['space_type_id']

4
resources/lang/en/validation.php

@ -87,7 +87,9 @@ return [
'fi_empty' => 'Floor cannot be empty',
'ri_empty' => 'Region cannot be empty',
'a_empty' => 'Parking space attribute cannot be empty',
'n_empty' => 'The parking number cannot be empty'
'n_empty' => 'The parking number cannot be empty',
'zh_plate' => 'Invalid license plate format; please check if it contains the prohibited letters I or O.',
'hk_plate' => 'Invalid license plate format; please check if it contains the prohibited letters I, O, or Q.'
],
'map' => [
'f_empty' => 'The floor number cannot be empty',

4
resources/lang/zh-CN/validation.php

@ -87,7 +87,9 @@ return [
'fi_empty' => '楼层不能为空',
'ri_empty' => '区域不能为空',
'a_empty' => '车位属性不能为空',
'n_empty' => '车位号码不能为空'
'n_empty' => '车位号码不能为空',
'zh_plate' => '车牌格式无效,请检查是否包含禁用的字母 I 或 O。',
'hk_plate' => '车牌格式无效,请检查是否包含禁用的字母 I、O 或 Q。'
],
'map' => [
'f_empty' => '楼层编号不能为空',

4
resources/lang/zh-TW/validation.php

@ -87,7 +87,9 @@ return [
'fi_empty' => '樓層不能為空',
'ri_empty' => '區域不能為空',
'a_empty' => '車位内容不能為空',
'n_empty' => '車位號碼不能為空'
'n_empty' => '車位號碼不能為空',
'zh_plate' => '車牌格式無效,請檢查是否包含已禁用的字母 I 或 O。',
'hk_plate' => '車牌格式無效,請檢查是否包含已禁用的字母 I、O 或 Q。'
],
'map' => [
'f_empty' => '樓層編號不能為空',

Loading…
Cancel
Save