加密后检查空字段

时间:2013-09-28 17:36:49

标签: php html encryption

我有这段代码:

<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.";
}

但由于加密,密码字段无论如何都会被填充。我需要的东西可以检查密码字段是否为空,然后按下密码被加密的按钮...任何想法?

2 个答案:

答案 0 :(得分:3)

确认输入内容的一种方法是在密码字段中使用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>

答案 1 :(得分:0)

因此,您无法动态散列密码,只需输入密码即可。

您可以使用.submit在哈希完成时发送表单

document.forms[form_name].submit()
相关问题