Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 6, 2018 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotsofbytes/5d279c2c2ce27197b83b902f9ca5d9af to your computer and use it in GitHub Desktop.
Save lotsofbytes/5d279c2c2ce27197b83b902f9ca5d9af to your computer and use it in GitHub Desktop.
[バリデーション:alpha_num] #laravel #L55 #validation

バリデーション:alpha_num

入力値が記号文字を含まない文字列かを判定。半角英数字の判定ではないことに注意。つまり日本語の文字列はどれもOK

laravel 5.5

gistの画面

以下の実行で、ダウンロードできます

$ git clone git@gist.github.com:5d279c2c2ce27197b83b902f9ca5d9af.git tests/Unit/Validations/AlphaNumeric
<?php
namespace Tests\Unit\Validations\AlphaNumeric;
use Tests\TestCase;
use Validator;
class AlphaNumericTest extends TestCase
{
/**
* @test
* @dataProvider provider_alpha_numeric
*/
public function alpha_numeric($input, $expected)
{
$v = Validator::make(
$input,
['field' => 'required|alpha_num']
);
$this->assertEquals($expected, $v->passes());
}
public function provider_alpha_numeric()
{
return [
[['field' => 'abcd'], true],
[['field' => 'ABCD'], true],
[['field' => '0'], true],
[['field' => '1'], true],
[['field' => 'ログイン'], true],
[['field' => 'ろぐいん'], true],
[['field' => '漢字'], true],
[['field' => 'ab cd'], false],
[['field' => '-'], false],
[['field' => '_'], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment