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

问题描述 投票:0回答:1

我有一个输入tel类型字段。即用于格式为x-xxxx-xxx的本地手机号码。有了这个,我就麻烦了。即使我输入了八位数字,也给出了数字验证错误。

验证规则

我尝试了多种方法来进行如下验证,但没有一种有效。

// 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" => "[email protected]"
  "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"
]

问题:

如何验证具有固定数字和格式的手机号码?

laravel laravel-validation
1个回答
0
投票

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

'移动'=>'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} $ /')],

© www.soinside.com 2019 - 2024. All rights reserved.