id 英数字3~32文字、パスワード英数字8~32文字のバリデーション
<?php
require_once("auth.php");
$mysqli = connect_mysql();
$status = "none";
if(!empty($_POST["username"]) && !empty($_POST["password"])){
if(!preg_match('/^[0-9a-zA-Z]{3,32}$/', $_POST["username"]))
$status = "error_username";
//パスワードのチェック
else if(!preg_match('/^[0-9a-zA-Z]{8,32}$/', $_POST["password"]))
$status = "error_password";
else{
$password = password_hash($_POST["password"], PASSWORD_DEFAULT);
$stmt = $mysqli->prepare("INSERT INTO users VALUES (?, ?)");
$stmt->bind_param('ss', $_POST["username"], $password);
if($stmt->execute())
$status = "ok";
else
$status = "failed";
}
}
?>
<head>
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
<script src="register_check.js"></script>
</head>
<h1>Jmail新規登録</h1>
<?php if($status == "ok"):?>
<p>登録完了</p>
<?php elseif($status == "failed"): ?>
<p>エラー:既に存在するユーザ名です。</p>
<?php elseif($status == "none"): ?>
<p>ユーザ名(英数字3~32文字)、推測されにくいパスワード(英数字8~32文字)を入力してください。</p>
<form method="POST" action="">
ユーザ名:<input type="text" name="username">
パスワード:<input type="password" name="password">
<input type="submit" value="登録">
</form>
<?php else: ?>
<p>hogehoge</p>
<?php endif; ?>
register_check.js
$(function(){
$("form").submit(function(){
if(!$("input[name=username]").val().match(/^[0-9a-zA-Z]{3,32}$/)
|| !$("input[name=password]").val().match(/^[0-9a-zA-Z]{8,32}$/)){
alert("入力エラー");
return false;
}
return true;
});
});
