Skip to content

Instantly share code, notes, and snippets.

@muhghazaliakbar
Created February 27, 2020 05:53
Show Gist options
  • Save muhghazaliakbar/0377f4d3a9e402b78e34ba8e5d0f2723 to your computer and use it in GitHub Desktop.
Save muhghazaliakbar/0377f4d3a9e402b78e34ba8e5d0f2723 to your computer and use it in GitHub Desktop.
Laravel validation to validate alpha, dash, and dot.
<?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.';
}
}
@muhghazaliakbar
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment