【问题标题】:Laravel mobile number validation with format and fixed digits具有格式和固定数字的 Laravel 手机号码验证
【发布时间】:2020-07-24 18:35:20
【问题描述】:

我有一个输入 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" => "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"
]

问题:

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

【问题讨论】:

  • 更改为输入数字或去掉验证中的数字,因为正则表达式为您提供了覆盖
  • 但是如果没有正则表达式呢?这也给出了同样的错误。我也尝试过删除数字并设置 max:8 仍然无效。
  • 是的,因为您将-(基于您的正则表达式)添加到输入中。要通过验证的数字应仅包含数字。
  • @ChamaraAbeysekara 我明白,但如果我不像第一个例子那样使用正则表达式怎么办?如果我只使用digits:8,那么它应该可以工作。不是吗?因为合而为一,我想修复数字/数字。我不想让用户输入超过 8 位数字。
  • @ChamaraAbeysekara 我已经用dd 更新了这个问题。我明白这个问题。它正在发送具有格式的数字。这就是为什么不通过验证,因为它不仅是数字,而且是破折号。那么,在这种情况下,如何使用格式强制电话总位数限制为 8?

标签: laravel laravel-validation


【解决方案1】:

我尝试了以下规则,将数字规则更改为字符串,因为 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}$/')],

【讨论】:

  • 更改最大值对用户没有意义。这会误导他们
  • 是的。你说的对。我已经使用自定义规则更新了我的答案。也许这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 2018-12-19
  • 2013-04-14
  • 2017-02-06
相关资源
最近更新 更多