0

我目前正在对一个表单进行一些非常基本的验证,该表单使用简单的 PHP 脚本将大量信息从 Web 表单发布到 SQL 数据库。PHP 脚本工作正常,并且值正在发布到数据库中,但是我编写的验证似乎被忽略了。我已经尝试修复它一段时间,但看不到任何明显的错误,但如果它是微不足道的,我深表歉意。

HTML:

<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php">
    <p class = "register">Desired Username*</p>
    <p class = "registerDesc">Between 1 and 20 characters long</p>                  
    <input type = "text" name = "username" class = "register">
    <p class = "register">Desired Password*</p>
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
    <input type = "password" name = "password" class = "register">
    <p class = "register">Confirm Password*</p>
    <input type = "password" name = "confirmPassword" class = "register">
    <p class = "register">Email Adress*</p>
    <input type = "text" name = "email" class = "registerEmail">
    <p class = "register">Bio</p>
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
    <textarea name = "bio" class = "registerBio"></textarea>
    <p><input type = "submit" class = "registerButton" value = "Register"></p>
</form>

JavaScript:

function ValidateRegistration()
{
    //username block
    var username=document.forms["registerForm"]["username"].value;
    if (username==null || username==" ")
    {
        alert("You must provide a username");
        return false;
    }       
    if (username.length>20 || username.length<1)
    {
        alert("Sorry, your username must be between 1 and 20 characters long.");
        return false;
    }
    //etc....
}
4

5 回答 5

2

正是您的代码对我有用:

创建以下 test.html 文件并在 chrome 中打开:

<html>
<head>
<script>
function ValidateRegistration()
{
    //username block
    var username=document.forms["registerForm"]["username"].value;
    if (username==null || username==" ")
    {
        alert("You must provide a username");
        return false;
    }       
    if (username.length>20 || username.length<1)
    {
        alert("Sorry, your username must be between 1 and 20 characters long.");
        return false;
    }
    //etc....
}
</script>
</head>
<body>
<form name = "registerForm" method = "POST" onsubmit = "return ValidateRegistration()" action = "createUser.php">
    <p class = "register">Desired Username*</p>
    <p class = "registerDesc">Between 1 and 20 characters long</p>                  
    <input type = "text" name = "username" class = "register">
    <p class = "register">Desired Password*</p>
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
    <input type = "password" name = "password" class = "register">
    <p class = "register">Confirm Password*</p>
    <input type = "password" name = "confirmPassword" class = "register">
    <p class = "register">Email Adress*</p>
    <input type = "text" name = "email" class = "registerEmail">
    <p class = "register">Bio</p>
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
    <textarea name = "bio" class = "registerBio"></textarea>
    <p><input type = "submit" class = "registerButton" value = "Register"></p>
</form>
</body>
</html>

提交一个空表单警报:

抱歉,您的用户名长度必须介于 1 到 20 个字符之间。

于 2012-12-28T23:19:27.227 回答
1

http://fiddle.jshell.net/hmariod/LRZkR/

<form name = "registerForm" method = "POST"  action = "createUser.php">
    <p class = "register">Desired Username*</p>
    <p class = "registerDesc">Between 1 and 20 characters long</p>                  
    <input type = "text" name = "username" class = "register">
    <p class = "register">Desired Password*</p>
    <p class = "registerDesc">We won't force securty onto you; between 6 and 255 characters long. No other criteria.</p>
    <input type = "password" name = "password" class = "register">
    <p class = "register">Confirm Password*</p>
    <input type = "password" name = "confirmPassword" class = "register">
    <p class = "register">Email Adress*</p>
    <input type = "text" name = "email" class = "registerEmail">
    <p class = "register">Bio</p>
    <p class = "registerDesc">Extra information such as hobbies, occupation, background information. Maximum 4096 characters. We can do this later.</p>
    <textarea name = "bio" class = "registerBio"></textarea>
    <p><input type = "button" onclick="ValidateRegistration();" class = "registerButton" value = "Register"></p>
</form>​

function ValidateRegistration(){
    try{
        var username=document.forms["registerForm"]["username"].value;
        if (username.length < 1)
        {
            alert("You must provide a username");
            return;
        }       
        if (username.length>20 || username.length<1)
        {
            alert("Sorry, your username must be between 1 and 20 characters long.");
            return;
        }
        document.forms["registerForm"].submit();
    }catch(er){
         alert(er);
    }
}​
于 2012-12-28T23:25:37.840 回答
0

我会摆脱 onsubmit 中的回报。

于 2012-12-28T22:55:48.427 回答
0

将您的代码放在 try/catch 块中,看看发生了什么。

try{
  //your code
}catch(er){
  alert(er);
}
于 2012-12-28T23:06:21.900 回答
0

我认为问题出在你的比较线上

if (username==null || username==" ") 

应该是

if (username==null || username=="")

我希望它能解决你的问题。请告诉我 ;-)

于 2013-03-01T15:59:03.980 回答