Laravel 手机号码格式和固定数字验证

Laravel mobile number validation with format and fixed digits

我有一个输入 tel 类型字段。即本地手机号码,格式为x-xxxx-xxx。有了这个,我遇到了麻烦失效。即使我输入八位数字,它也会出现数字验证错误。

验证规则

我尝试了多种验证方法如下,但 none 有效。

// without regex
'mobile'     => 'nullable|digits:8',

// regex one
'mobile'     => 'nullable|regex:/^([0-9\s\-\+\(\)]*)$/|digits:8',

// regex two

'mobile'     => 'nullable|regex:/^\d{1}-\d{4}-\d{3}$/|digits:8',

输入标记

我正在使用 inputmask js 插件,以备不时之需。

<div class="form-group"><label for="mobile">{{__('admin.user.profile.mobile')}}</label>
    <input type="tel" class="form-control" id="mobile" name="mobile"
           value="{{ isset($user) ? $user->profile->mobile : old('mobile') }}"
           placeholder="{{__('admin.user.profile.mobile')}}" data-inputmask="'mask': '9-9999-999'">
    @error('mobile')
    <span class="invalid-feedback d-block" role="alert"><strong>{{ $message }}</strong></span>
    @enderror
</div>

验证错误消息

The mobile must be 8 digits.

DD

所以这里我可以理解原因,可能是发送号码格式。那么现在我该如何验证它呢?

array:14 [▼
  "_token" => "U5F3BkiAryYkaz75R1oraUfcx3ydz6bv6Ac7mw7K"
  "_method" => "PUT"
  "email" => "super@test.com"
  "password" => null
  "password_confirmation" => null
  "role" => "super"
  "first_name" => "Katheryn"
  "last_name" => "Jones"
  "mobile" => "4-6655-499"
  "city" => "Taylorbury"
  "facebook" => "http://fritsch.com/numquam-repudiandae-consectetur-sequi-suscipit-numquam"
  "twitter" => "http://jacobi.com/"
  "youtube" => "https://dubuque.org/explicabo-autem-corporis-distinctio.html"
  "instagram" => "https://www.franecki.com/eos-non-nostrum-quia-commodi-ex-totam"
]

Question:

How to validate mobile numbers with fixed digits and format?

我尝试了以下规则,我将数字规则更改为字符串,因为 laravel 没有数字加破折号的规则。

'mobile' => 'nullable|string|min:10|max:10|regex:/^\d{1}-\d{4}-\d{3}$/',

希望以上解决方案对您有所帮助。

更新

为了向用户显示适当的错误消息,您需要创建自定义规则

运行以下命令

php artisan make:rule NumericDash

使用以下内容更新 NumericDash.php 文件

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class NumericDash implements Rule
{
    private $message = "The :attribute format is invalid.";
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($digitsWithoutDash, $regex)
    {
        $this->digitsWithoutDash = $digitsWithoutDash;
        $this->regex = $regex;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (strlen(preg_replace('/[^0-9]/', '', $value)) !== $this->digitsWithoutDash) {
            $this->message = "The :attribute must include {$this->digitsWithoutDash} digits.";
            return false;
        }
        if (!preg_match($this->regex, $value)) {
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }
}

并使用如下规则

'mobile' => ['nullable', 'string', new NumericDash(8, '/^\d{1}-\d{4}-\d{3}$/')],