Skip to content

Instantly share code, notes, and snippets.

@ciaranpflanagan
Last active December 28, 2020 20:51
Show Gist options
  • Save ciaranpflanagan/1f0ffdd4bda088cc81210040fbb79bde to your computer and use it in GitHub Desktop.
Save ciaranpflanagan/1f0ffdd4bda088cc81210040fbb79bde to your computer and use it in GitHub Desktop.
A custom Eircode validation rule for laravel. Using the REGEX from Manwal's answer on this stackoverflow question. https://stackoverflow.com/questions/33391412/validation-for-irish-eircode
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Eircode implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return preg_match('/^[AC-Y]{1}[0-9]{1}[0-9W]{1}[ \-]?[0-9AC-Y]{4}$/', $value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Eircode not valid';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment