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

确认输入内容的一种方法是在密码字段中使用 HTML5 required 属性,如下所示

<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 回答
0

所以,你不能动态地散列你的密码,它只是在输入总密码时。

您可以.submit在哈希完成后发送表单

document.forms[form_name].submit()
于 2013-09-28T17:51:26.630 回答