重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

【GOLF me!】初月無料お試し

正規表現で下記のパスワードをブロックしたいのですが、どうすれば良いですか。

・8文字以上の半角英数字(記号入り)
・パスワード中に必ず、英字、数字、記号を1文字を入れる

A 回答 (6件)

<script type="application/javascript; version=1.8">



var check =
 (function (func, aryReg)
  (function (password)
   aryReg.every (func, password)))
 ((function (r) r.test (this)),
  [
   /[\u0021-\u007e]{8,16}/,
   /[0-9]/,
   /[a-z]/i,
   /[\u0021-\u002f\u003a-\u003f\u005b-\u0060\u007b-\u007e]/
  ]);

alert(check('asssb.c0'));
</script>
    • good
    • 0
この回答へのお礼

ありがとうございます。

お礼日時:2010/12/06 19:42

No.1,2の回答者です。


パスワード中に必ず、英字、数字、記号が1文字以上あって、
英字、数字、記号が8文字以上で、その他の文字を含まないの
正規表現の一発パターン

/(?=(?=.*\d)(?=.*[a-z])(?!.*[^\x00-\x7F]))[\x00-\x7F]{8,}?/i

でどうだ。

(参考)

http://www.php.net/manual/ja/regexp.reference.as …

「言明 (assertion) とは、カレントのマッチング位置の直前・直後の文字に対
する テストであり、文字を消費 (consume)〔つまり文字自体にマッチ〕しません。」

「先読み言明 (lookahead assertion) は、 肯定の言明 (positive assertion)
の場合 (?= で始まり、 否定の言明 (negative assertion) の場合 (?! で始
まります。」
    • good
    • 0
この回答へのお礼

ありがとうございます。

お礼日時:2010/12/06 19:45

ついでに、サーバー側でも(PHP)



<?php header("Content-type:text/html;charset=UTF-8");
if(isset($_POST['password']) && (preg_match('/[[:ascii:]]{8,}?/',$_POST['password']) > 0)){
print_r('OK');
}else{ ?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Pass Word Check</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" onsubmit="return check(this);">
<input type="password" name="password">
<button type="submit">送信</button>
</form>
<script type="text/javascript">
function check(frm){
var password = frm.elements["password"].value;
var regex = /[\x00-\x7F]{8,}?/;
if(regex.exec(password))
return true;
else alert("NG");
return false;
}
</script>
</body>
</html>
<?php
}
?>
    • good
    • 0

javascriptの例



<form onsubmit="return check(this);" action="#">
<input name="password">
<button type="submit">送信</button>
</form>
<script type="text/javascript">
function check(frm){
var password = frm.elements["password"].value;
var regex = /[\x00-\x7F]{8,}?/;
if(regex.exec(password)) return true;
else alert("NG");return false;
}
</script>
    • good
    • 0

>英字、数字、記号を1文字を入れる



定義が微妙にわかりにくいので例示がほしいところ

「abc123_*」みたいに必ず1文字以上入っていないといけないのでしょうか?
また、記号とはどこまで使用可能なのでしょうか?

この回答への補足

「abc123_*」みたいに必ず1文字以上入っていないといけないのでしょうか?
また、記号とはどこまで使用可能なのでしょうか?

ご指摘の通りです。

補足日時:2010/12/02 16:20
    • good
    • 0

対象言語くらい書きましょう

この回答への補足

javaScriptです。すみません。

補足日時:2010/12/02 16:20
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!