Raymond Baghumian
Posted on April 21, 2024
by default we using request laravel which validates user input data.
but we want Create Custom Validation rules which by default request validation we don't use.
with under command create two file ValiMobile and ValiPassword:
php artisan make:rule ValidMobile
php artisan make:rule ValidPassword
I got for Regex from site under helping.
https://ihateregex.io/
Code VailMobile :
class ValidMobile implements ValidationRule
{
public function __construct()
{
//
}
public function passes($attribute, $value): bool|int
{
return preg_match('/^9[0-9]{9}$/', $value);
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!preg_match('/^9[0-9]{9}$/', $value)) {
$fail('Your ' . $attribute . 'number is 10 digits and enter without zero.');
}
}
}
Code VailPassword :
class ValidPassword implements ValidationRule
{
public function __construct()
{
//
}
public function passes($attribute, $value): bool|int
{
return preg_match('/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,}$/', $value);
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!preg_match('/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,}$/', $value)) {
$fail('The type of' . $attribute . 'is inappropriate and must be a combination of uppercase, lowercase letters and numbers.');
}
}
}
at the end Calling Two File VailMobile and VailPassword:
use App\Rules\ValidMobile;
use App\Rules\ValidPassword;
return Validator::make($data,
[
'mobile' => ['nullable', 'string', 'unique:users', new ValidMobile()],
'password' => ['required', 'string', 'confirmed', new ValidPassword()],
]
);
Good luck :)
💖 💪 🙅 🚩
Raymond Baghumian
Posted on April 21, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
laravel PHP is a Single-Threaded Language, So How Does Laravel Handle Queue Jobs Asynchronously?
November 28, 2024