公式: https://readouble.com/laravel/8.x/ja/validation.html#custom-validation-rules
$ php artisan make:rule PhoneRule
app/Rules/PhoneRule.php
1 2 3 4 5 6 7 8 9 10 11 12 | public function passes( $attribute , $value ) { if ( $value ){ return preg_match( '/^(([0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4})|([0-9]{8,11}))$/' , $value ); } else { return true; } } public function message() { return trans( 'validation.phone' ); } |
$ php artisan make:rule ZipcodeRule
app/Rules/ZipcodeRule.php
1 2 3 4 5 6 7 8 9 10 11 12 | public function passes( $attribute , $value ) { if ( $value ){ return preg_match( '/^(([0-9]{3}-[0-9]{4})|([0-9]{7}))$/' , $value ); } else { return true; } } public function message() { return trans( 'validation.zipcode' ); } |
/resources/lang/ja/validation.php
1 2 3 4 5 6 7 | 'phone' => ':attributeはハイフン有りか無しで半角英数字8~11個の数字で入力してください。' , 'zipcode' => ':attributeはハイフン有りか無しで半角英数字7個の数字で入力してください。' , 'attributes' => [ 'phone' => '電話番号' , 'zipcode' => '郵便番号' , ], |
OK、次はEmail。HTML5のE-mailのバリデーションがどうなってるかだな。