doumaji6215 2012-12-28 22:48
浏览 51
已采纳

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

I'm currently working on some very basic validation for a form that posts a bunch of information from a web form to an SQL database with a simple PHP script. The PHP script is working fine, and the values are being posted to the database, however the validation I have written appears to be ignored. I've attempted to fix it for a while now and can't see any glaring mistakes, but apologies if it's anything trivial.

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条回答 默认 最新

  • douya2006 2012-12-28 23:19
    关注

    Exactly your code is working for me:

    Created following test.html file and opened in 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>
    

    Submitting an emtpy form alerts:

    Sorry, your username must be between 1 and 20 characters long.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀