具有格式和固定数字的Laravel手机号码验证
我有一个输入具有格式和固定数字的Laravel手机号码验证,laravel,laravel-validation,Laravel,Laravel Validation,我有一个输入teltype字段。这是用于格式为x-xxxx-xxx的本地移动电话号码。有了这一点,我在无效性方面遇到了麻烦。即使我输入八位数字,它也会给出数字验证错误 验证规则 我尝试了多种验证方法,如下所示,但没有一种有效 // without regex 'mobile' => 'nullable|digits:8', // regex one 'mobile' => 'nullable|regex:/^([0-9\s\-\+\(\)]*)$/|digits:8
tel
type字段。这是用于格式为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',
输入标记
我正在使用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>
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"
]
问题:
如何验证固定数字和格式的手机号码
我尝试了以下规则,将数字规则改为字符串,因为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;
}
}
我尝试了以下规则,将数字规则更改为字符串,因为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;
}
}
更改为键入number或删除validate中的数字,因为regex已经为您涵盖了这一点,但是如果没有regex呢?这也会产生同样的错误。我还尝试删除数字并设置最大值:8仍然无法验证。是的,因为您正在将-
(基于您的正则表达式)添加到输入中。要通过验证,数字应该只包含数字,而不包含任何其他内容。@ChamaraAbeysekara我理解,但如果我没有像第一个示例中那样使用正则表达式,该怎么办?如果我只使用数字:8
,那么它应该可以工作。不是吗?因为这一切合在一起,我想修正数字。我不允许用户输入超过8位的数字。@chamarabeysekara我已经用dd
更新了问题。我理解这个问题。它正在发送格式为的数字。这就是为什么不通过验证的原因,因为它不仅是数字,而且是破折号。那么,在这种情况下,我如何强制使用格式将电话总数字限制为8?更改为键入数字或删除验证中的数字,因为regex为您提供了这一功能,但如果没有regex呢?这也会产生同样的错误。我还尝试删除数字并设置最大值:8仍然无法验证。是的,因为您正在将-
(基于您的正则表达式)添加到输入中。要通过验证,数字应该只包含数字,而不包含任何其他内容。@ChamaraAbeysekara我理解,但如果我没有像第一个示例中那样使用正则表达式,该怎么办?如果我只使用数字:8
,那么它应该可以工作。不是吗?因为这一切合在一起,我想修正数字。我不允许用户输入超过8位的数字。@chamarabeysekara我已经用dd
更新了问题。我理解这个问题。它正在发送格式为的数字。这就是为什么不通过验证的原因,因为它不仅是数字,而且是破折号。那么,在这种情况下,我如何才能强制使用该格式将电话总位数限制为8?更改最大值对用户来说没有意义。这会误导他们是的。你是对的。我已使用自定义规则更新了我的答案。也许这会有帮助。更改最大值对用户来说没有意义。这会误导他们是的。你是对的。我已使用自定义规则更新了我的答案。也许这会有帮助。