dongqiao9394 2011-08-08 21:17
浏览 48
已采纳

在提交数据之前验证表单

I'm trying to validate a form before posting the data. Basically the form asks for a code. After the user clicks the submit button, the javascript below checks if the code only contains numbers and has a length of 6 digit. If the code is valid, then an ajax call is made calling the page verifybkcd.php, which runs a query and checks if the input code has associated with an account. Somehow the form is always submitted no matter what code I put.It seemed the ajax call has never been made because none of the alerts inside the call had been triggered. (I'm sure the path of the file verifybkcd.php is correct). Any ideas?

     $(document).ready(function(){

    $("#bksubmit").click(function(e){


       var bkcode = $("#bkcode").val();

       var bkcode_format =/^\d{6}$/;
       if(bkcode_format.test(bkcode)){

          $("#err-box").text("");
          $("#recovform").submit(function(e){

            alert("alert on submit");


            $.post("accounts/verifybkcd.php",{bcd:bkcode}, function(data){
                    alert("alert inside post");
                    if(data !=''){
                         alert("alert on code exists");
                        e.preventDefault();
                        $("#err-box").text("No account found with that book code. Please try again.");

                    }
                    else{

                        alert("alert on valid");
                        $("#err-box").text("");


                    }
            });

        });

       }
       else{

         $("#err-box").text("Please enter a valid book code above");
         e.preventDefault();
       }


    });

The following is taken from the php file which has the form

     <div id="container">
     <?php
    if($_SERVER['QUERY_STRING']==''){
     ?>
    <div class="title"><h1>Forgot your password?</h1></div>
    <div class="description"><p>To reset your password, enter the book code that you used when you registered your account.</p></div>

        <form id="recovform" name="recovform" method="post" action="recovery.php?verifyuser" >
            <div id="bkbox">
                <label for="bkcode">Book Code:</label>
                <input id="bkcode" name="bkcode" type="text" />
                <input id="bksubmit" name="submit" type="submit" value="Submit"/>
            </div>
           <div id="err-box"></div>
        </form>
     <?php
    } 
     else if($_SERVER['QUERY_STRING']=='verifyuser'){

     ?>
       <div class="title"><h1>verify user</h1></div>
       <div class="description"><p>emails below</p></div>
    <?php
     }
     else{
        echo "<META   HTTP-EQUIV=REFRESH   CONTENT='0;URL=../err/404.php'>";
     }
    ?>
</div>

BTW, I'm sure there's nothing wrong with verifybkcd.php file. I've tested it with different codes. It will only return a string when there're no accounts associated with the input code.


Problem solved. I replaced the name of the submit button with something else. Then it worked like magic. Also I've made a little change on the javascript as follows. It seems jquery won't submit the form if you name the submit button "submit". BTW I aslo changed the type of the submit button to "button" instead of "submit"

    $(document).ready(function(){

    $("#bksubmit").click(function(e){


       var bkcode = $("#bkcode").val();

       var bkcode_format =/^\d{6}$/;
       if(bkcode_format.test(bkcode)){

          $("#err-box").text("");

            $.post("accounts/verifybkcd.php",{bcd:bkcode}, function(data){
                    if(data !=''){   
                        $("#err-box").text("No account found with that book code. Please try again.");
                    }
                    else{

                        $("#err-box").text("");
                        $("#recovform").trigger("submit");

                    }
            });

       }
       else{

         $("#err-box").text("Please enter a valid book code above");
       }


    });



    });
  • 写回答

2条回答 默认 最新

  • dsymx68408 2011-08-08 21:26
    关注

    First place if you dont want to submit the form then you should return false in the submit button click handler. Check if this below condition satisfied or not.

    $(document).ready(function(){
    

    //This is to prevent the form to be submitted $("#recovform").submit(function(e){ e.preventDefault(); });

    $("#bksubmit").click(function(e){
    
       var bkcode = $("#bkcode").val();
    
       var bkcode_format =/^\d{6}$/;
       if(bkcode_format.test(bkcode)){
    
          $("#err-box").text("");
          //$("#recovform").submit(function(e){
    
            //alert("alert on submit");
    
    
            $.post("accounts/verifybkcd.php",{bcd:bkcode}, function(data){
                    alert("alert inside post");
                    if(data !=''){
                        //alert("alert on code exists");
                        //e.preventDefault();
                        $("#err-box").text("No account found with that book code. Please try again.");
    
                    }
                    else{
    
                        //alert("alert on valid");
                        $("#err-box").text("");
    
                        $("#recovform").unbind('submit').submit(); 
                    }
            });
    
        //});
    
       }
       else{
    
         $("#err-box").text("Please enter a valid book code above");
         return false;
       }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图