JavaScript表单验证 - 函数未在提交时运行。

时间:2012-12-28 22:48:10

标签: php javascript html web

我目前正在为一个表单进行一些非常基本的验证,该表单使用简单的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....
}

5 个答案:

答案 0 :(得分: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>

提交emtpy表单提醒:

  

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

答案 1 :(得分: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);
    }
}​

答案 2 :(得分:0)

我会在onsubmit中摆脱回报。

答案 3 :(得分:0)

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

try{
  //your code
}catch(er){
  alert(er);
}

答案 4 :(得分:0)

我认为问题出在比较线上

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

应该是

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

我希望它能解决你的问题。请让我知道; - )