0

私はこのコードの塊を持っています:

<form action="register.php" method="post">
     Email: <input type="text" name="email" /><br />
     Username: <input type="text" name="username" /><br />
     Password: <input type="password" name="p" id="password" /><br />
     <input type="button" value="Register" onclick="formhash(this.form, this.form.password);"   />
 </form>

ご覧のとおり、ボタンをクリックすると、パスワードを暗号化する関数が呼び出されます。私の問題は、パスワードを確認する前に暗号化によってパスワードが暗号化されるため、ユーザーがパスワード フィールドに何かを書いたかどうかを確認できないことです。私は通常、次のようにチェックします。

<?php if (empty($_POST['password'])) {
echo "You forgot to fill the password field.";
}

しかし、暗号化のため、パスワード フィールドは何があっても入力されます。パスワードが暗号化されるボタンが押される前に、パスワードフィールドが空かどうかを確認できるものが必要です...何かアイデアはありますか?

4

2 に答える 2

3

何かが入力されたことを確認する 1 つの方法は、次のようにパスワード フィールドで HTML5 の必須属性を使用することです。

<input type="password" name="yourname" id="yourid" required />

これにより、パスワード フィールドに何かが入力されたことが常に確認されます。これが役立つかもしれません。

これを試してください:

<html>
<head>
<title>Form validation test</title>
<script type="text/javascript">
function validateAndEncrypt()
{
var email = document.getElementById("email");
var name  = document.getElementById("name");
var pass  = document.getElementById("password");
//if you want to validate the password only check the value of password field only
if(email.value== "" || name.value == "" || pass.value== "")
{
    alert("One of the fields is empty the script cannot continue.")
    return false;
}
else
{
    // you can encrypt your password here
    alert("Everyting is fine, now we can proceed further");
    return true;
}
}
//if you want to check the password field before hitting the button
function checkPassword()
{
var pass  = document.getElementById("password");
if(pass.value=="")
{
    alert("The password field is empty");
    return false;
}
return true;
}
</script>
</head>
<body>
<form action="register.php" method="post">
 Email: <input type="text" name="email" id="email" /><br />
 Username: <input type="text" name="username" id="name"/><br />
 Password: <input type="password" name="password" id="password" onblur="return checkPassword();"required /><br />
 <input type="submit" value="Register" onclick="return validateAndEncrypt();"   />
 </form>
 </body>
 </html>
于 2013-09-28T18:00:58.043 に答える