Laravel 或验证 URL 或 IP
Laravel OR Validation URL or IP
我有一个表单字段,我想验证输入的值是否有效 url 或 IP 地址。
我创建了一个自定义表单请求验证来验证 url(来自 laravel 的可用 url 验证还不够)并且工作正常。
但是我如何更改我的验证以检查它是否是有效的 url 或有效的 ip?只有当两个验证都失败时,它才应该失败。
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CheckDomain extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
*
* Manipulate Input Domain before Validation
*
*/
protected function prepareForValidation()
{
// Remove HTTP - HTTPS
$this->domain = str_replace([
'http://', 'https://'
], '', $this->domain);
$this->domain = strtok($this->domain, '/'); // Remove Slashes
$this->domain = strtolower($this->domain); // Make Lowercase
// Bring Together
$this->merge([
'domain' => $this->domain
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'domain' => [
'required', 'regex:^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^'
]
];
}
}
您可以使用 this 正则表达式
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
你的代码最终应该是这样的:
public function rules()
{
return [
'domain' => ['required', 'regex:^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$']
];
}
我有一个表单字段,我想验证输入的值是否有效 url 或 IP 地址。
我创建了一个自定义表单请求验证来验证 url(来自 laravel 的可用 url 验证还不够)并且工作正常。
但是我如何更改我的验证以检查它是否是有效的 url 或有效的 ip?只有当两个验证都失败时,它才应该失败。
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CheckDomain extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
*
* Manipulate Input Domain before Validation
*
*/
protected function prepareForValidation()
{
// Remove HTTP - HTTPS
$this->domain = str_replace([
'http://', 'https://'
], '', $this->domain);
$this->domain = strtok($this->domain, '/'); // Remove Slashes
$this->domain = strtolower($this->domain); // Make Lowercase
// Bring Together
$this->merge([
'domain' => $this->domain
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'domain' => [
'required', 'regex:^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^'
]
];
}
}
您可以使用 this 正则表达式
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
你的代码最终应该是这样的:
public function rules()
{
return [
'domain' => ['required', 'regex:^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$']
];
}