0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】大文字・小文字・数字が1文字以上含まれるパスワードの生成方法

Posted at

概要

下記の条件を満たすパスワードの生成

  • 大文字が1文字以上含まれる
  • 小文字が1文字以上含まれる
  • 数字が1文字以上含まれる
  • 文字数は8文字

環境

  • Laravel Sailでの環境構築
    • Laravel 10.30.1
    • PHP 8.2.12

実際の記述

private function generatePassword(): string
{
    $password = Str::password(8);

    if (preg_match('/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$/', $password)) {
        return $password;
    }

    return $this->generatePassword();
}

補足

  • Controller内のprivateメソッドに定義した際の記述
    • ハッシュ化(Hash::make())はメソッドの呼び出し先で処理
  • 他の文字数に設定したい場合は下記2箇所の8を任意の数に書き換える
    • Str::password(8)
    • '/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$/'
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?