laravel国家代码和电话号码验证

1bqhqjot  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(372)

countrycoutry代码数字索引211KSA39USA19

public function rules(): array
    {
        return [
    '*.sender_phone_code' => ['required', 'integer', 'digits:3'],
    '*.sender_phone_number'   => ['required', 'integer', 'digits:9']
]}

如何在验证规则laravel中验证上述电话号码和国家代码验证。

1szpjjfi

1szpjjfi1#

我用 giggsey/libphonenumber-for-php 为此:

public function rules(): array
    {
        return [
    '*.sender_phone_code' => ['required', 'integer', 'digits:3'],
    '*.sender_phone_number'   => ['required', 'integer', 'digits:9', function ($attribute, $value, $fail) {
            $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
            try {
                $phoneNumberProto = $phoneUtil->parse($value);
            } catch (\libphonenumber\NumberParseException $e) {
                $fail('The '.$attribute.' is invalid.');
            }
            if (!$phoneUtil->isValidNumber($phoneNumberProto)) {
                $fail('The '.$attribute.' is invalid.');
            }
        }]
]}

相关问题