会員登録フォームのパスワード強度をチェックする方法【HTML/CSS/JavaScript・サンプルコード付き】
フォーム
会員登録フォームのパスワード強度をチェックする方法
会員登録フォームのパスワード強度をチェックする方法を紹介します。本記事では、HTML・CSS・JavaScriptを使用して、入力されたパスワードの強度をリアルタイムで判定し、その結果を画面へ表示する方法を解説します。会員登録フォームやアカウント作成フォームで利用できる機能で、ユーザーに安全性の高いパスワードを設定してもらいたい場合に役立ちます。
このサンプルコードでは、入力されたパスワードの文字数や文字の種類を確認し、「弱い」「普通」「強い」「とても強い」といった強度をリアルタイムで表示します。入力するたびに判定結果が更新されるため、ユーザーは現在のパスワードの強度を確認しながら登録作業を進めることができます。HTMLでフォームを作成し、CSSで表示を整え、JavaScriptで強度判定を行うシンプルな構成です。
パスワード強度チェックは、会員登録フォーム、ユーザー登録フォーム、パスワード変更フォームなどで広く利用されています。推測されやすい短いパスワードの使用を減らし、より安全なパスワード設定を促せるため、フォームの利便性向上だけでなくセキュリティ対策としても有効です。実際に動作するサンプルコードを参考にしながら、パスワード強度チェック機能を導入したい方にもおすすめです。
コードについて
本記事のコードはサンプルコードです。ご利用前に必ず動作確認を行ってください。
免責事項
本コードの利用により発生した損害について、当サイトは一切の責任を負いません。
デモ
会員登録フォーム
入力したパスワードの強度をリアルタイムで判定するサンプルです。
判定基準
弱い:8文字未満
普通:8文字以上
強い:8文字以上 + 英大文字 + 英小文字 + 数字
とても強い:8文字以上 + 英大文字 + 英小文字 + 数字 + 記号
※このデモは表示例です。リアルタイム判定は、HTML・CSS・JavaScriptを別ファイルで保存した配布コードで動作します。
コードをコピーして使おう!
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>会員登録フォームのパスワード強度チェック</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="member-registration-password-strength-check_wrap">
<div class="member-registration-password-strength-check_title">
会員登録フォーム
</div>
<p class="member-registration-password-strength-check_text">
入力したパスワードの強度をリアルタイムで表示します。
</p>
<div class="member-registration-password-strength-check_field">
<label class="member-registration-password-strength-check_label" for="member-registration-password-strength-check-password">
パスワード
</label>
<input
class="member-registration-password-strength-check_input"
type="text"
id="member-registration-password-strength-check-password"
name="password"
placeholder="パスワードを入力"
>
<div class="member-registration-password-strength-check_result">
パスワードを入力してください。
</div>
</div>
<div class="member-registration-password-strength-check_rule">
<div class="member-registration-password-strength-check_rule-title">
判定基準
</div>
弱い:8文字未満<br>
普通:8文字以上<br>
強い:8文字以上 + 英大文字 + 英小文字 + 数字<br>
とても強い:8文字以上 + 英大文字 + 英小文字 + 数字 + 記号
</div>
</div>
<script src="script.js"></script>
</body>
</html>
body{
margin:0;
font-family:sans-serif;
background:#ffffff;
}
*{
box-sizing:border-box;
}
.member-registration-password-strength-check_wrap{
max-width:520px;
margin:40px auto;
padding:32px;
border:1px solid #e5e7eb;
border-radius:18px;
background:#f8fafc;
}
.member-registration-password-strength-check_title{
margin-bottom:12px;
font-size:22px;
font-weight:700;
text-align:center;
}
.member-registration-password-strength-check_text{
margin-bottom:24px;
color:#4b5563;
text-align:center;
line-height:1.8;
}
.member-registration-password-strength-check_field{
display:flex;
flex-direction:column;
gap:8px;
}
.member-registration-password-strength-check_label{
font-weight:700;
color:#111827;
}
.member-registration-password-strength-check_input{
width:100%;
padding:14px 16px;
border:1px solid #d1d5db;
border-radius:10px;
background:#ffffff;
font-size:16px;
color:#111827;
outline:none;
}
.member-registration-password-strength-check_input:focus{
border-color:#0b6bff;
box-shadow:0 0 0 3px rgba(11,107,255,0.15);
}
.member-registration-password-strength-check_result{
min-height:20px;
font-size:14px;
font-weight:700;
}
.member-registration-password-strength-check_result.is-weak{
color:#dc2626;
}
.member-registration-password-strength-check_result.is-normal{
color:#d97706;
}
.member-registration-password-strength-check_result.is-strong{
color:#2563eb;
}
.member-registration-password-strength-check_result.is-very-strong{
color:#16a34a;
}
.member-registration-password-strength-check_rule{
margin-top:18px;
padding:16px;
border-radius:12px;
background:#ffffff;
color:#4b5563;
font-size:13px;
line-height:1.8;
}
.member-registration-password-strength-check_rule-title{
margin-bottom:6px;
color:#111827;
font-weight:700;
}
// パスワード入力欄を取得
const memberRegistrationPasswordStrengthCheckInput=document.querySelector("#member-registration-password-strength-check-password");
// 判定結果を表示する要素を取得
const memberRegistrationPasswordStrengthCheckResult=document.querySelector(".member-registration-password-strength-check_result");
// パスワード入力時の処理
memberRegistrationPasswordStrengthCheckInput.addEventListener("input",function(){
const password=memberRegistrationPasswordStrengthCheckInput.value;
const hasUppercase=/[A-Z]/.test(password);
const hasLowercase=/[a-z]/.test(password);
const hasNumber=/[0-9]/.test(password);
const hasSymbol=/[^A-Za-z0-9]/.test(password);
memberRegistrationPasswordStrengthCheckResult.classList.remove(
"is-weak",
"is-normal",
"is-strong",
"is-very-strong"
);
if(password===""){
memberRegistrationPasswordStrengthCheckResult.textContent="パスワードを入力してください。";
return;
}
/*
判定基準のカスタマイズ方法
if(password.length<8)
の数字を変更すると、最低文字数を変更できる。
hasUppercase → 英大文字
hasLowercase → 英小文字
hasNumber → 数字
hasSymbol → 記号
条件を追加・削除すると判定基準を変更できる。
HTML側の判定基準表示も同じ内容へ変更する。
*/
if(password.length<8){
memberRegistrationPasswordStrengthCheckResult.textContent="弱い";
memberRegistrationPasswordStrengthCheckResult.classList.add("is-weak");
return;
}
if(hasUppercase&&hasLowercase&&hasNumber&&hasSymbol){
memberRegistrationPasswordStrengthCheckResult.textContent="とても強い";
memberRegistrationPasswordStrengthCheckResult.classList.add("is-very-strong");
return;
}
if(hasUppercase&&hasLowercase&&hasNumber){
memberRegistrationPasswordStrengthCheckResult.textContent="強い";
memberRegistrationPasswordStrengthCheckResult.classList.add("is-strong");
return;
}
memberRegistrationPasswordStrengthCheckResult.textContent="普通";
memberRegistrationPasswordStrengthCheckResult.classList.add("is-normal");
});
ファイル構成と設置方法
このサンプルでは、HTML・CSS・JavaScriptを別ファイルで管理します。index.html、style.css、script.js を同じフォルダへ保存してください。
sample/
├── index.html
├── style.css
└── script.js
index.html をブラウザで開くと、会員登録フォームのパスワード強度チェックを確認できます。パスワードを入力すると、「弱い」「普通」「強い」「とても強い」の判定結果がリアルタイムで表示されます。
判定基準を変更したい場合は、script.js 内の文字数や、英大文字・英小文字・数字・記号の条件を編集してください。条件を変更した場合は、index.html 内の「判定基準」説明文も同じ内容に修正してください。
会員登録フォームのパスワード強度をチェックする方法【HTML/CSS/JavaScript・サンプルコード付き】
コメント