Created
February 27, 2020 05:53
-
-
Save muhghazaliakbar/0377f4d3a9e402b78e34ba8e5d0f2723 to your computer and use it in GitHub Desktop.
Laravel validation to validate alpha, dash, and dot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class AlphaDashDot implements Rule | |
{ | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
if (! is_string($value) && ! is_numeric($value)) { | |
return false; | |
} | |
return preg_match('/^[\pL\pM\pN_-].+$/u', $value) > 0; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'The :attribute may only contain letters, numbers, dashes, underscores and dots.'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@muhghazaliakbar @JCarlosR
Thanks for this, exactly what I needed 😁