在错误的验证码验证期间无法停止表单提交

时间:2014-09-23 05:21:46

标签: php jquery

错误的验证表单提交后 1. Jquery是:

function validate()
{
 if( document.myForm.capcode.value == "" )
   {
     alert( "Please type Code!" );
     document.myForm.capcode.focus() ;
	 return false 
   }
   else if( document.myForm.capcode.value != "" ){
       $.ajax
                ({
                    type: "POST",
                    url: "checkcaptcha.php",
                    data: "captcha="+document.myForm.capcode.value,
                    success: function(msg)
                        {
                        if($.trim(msg) == '2'){
                            alert('Entered code is wrong');
                            reloadCaptcha()
                            return false;
                        }
                    }
                })
           
   }
   
   function reloadCaptcha()
    {
        jQuery('#siimage').prop('src', 'securityimage/securimage_show.php?sid=' + Math.random());
    }
  return( true );
}

2. Html start hare I am using secure image php cpatcha, captcha validation work but during wrong validation form should not be submitted :
2. Html start hare I am using secure image php cpatcha, captcha validation work but during wrong validation form should not be submitted :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="frmSignUp" action="/account/signup?a=1" name="myForm" method="post" onsubmit="return(validate());">
<table width="390" border="0" cellspacing="0" cellpadding="0" align="right"><td colspan="3" >
<div style="float: right;margin-right:26px;width:200px;">
<img id="siimage" align="left" style="padding-right: 5px; border: 0" src="/securityimage/securimage_show.php?sid={/application/random}"  />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="19" height="19" id="SecurImage_as3" align="middle">
                                            		<param name="wmode" value="opaque">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="http://cheersoye.com/securityimage/securimage_play.swf?audio=/securityimage/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="http://cheersoye.com/securityimage/securimage_play.swf?audio=http://cheersoye.com/securityimage/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5" quality="high" bgcolor="#ffffff" width="19" height="19" name="SecurImage_as3" wmode="opaque" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object><br/>
<a tabindex="-1" style="border-style: none" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = '/securityimage/securimage_show.php?sid=' + Math.random(); return false"><img src="<?php echo IMAGE_URL; ?>/refresh.gif" alt="Reload Image" border="0" onclick="this.blur()" align="bottom" /></a></div></td></tr>
<tr><td class="log_label" colspan="2">Enter Code:</td>
<td><div id="" class="form-group">
<input name="confirmationcode" type="text" id="capcode" class="log_input" value="" /></div></td></tr>
<tr><td colspan="3" style="text-align:center;">
<input type="submit" value="Register" style="height:35px;" class="small_btn" /></td></tr></table>
</form>

3 个答案:

答案 0 :(得分:1)

您需要以这种方式执行此操作,Demo

var validated = false;
$("#frmSignUp").on("submit", function (e) {
    if (!validated) {
        e.preventDefault(); <-- this will stop the submit.
        var captchaCode = $("#capcode").val();
        if (captchaCode == "") {
            alert("Please type Code!");
            $("#capcode").focus();
            return false
        } else {
            $.ajax({
                type: "POST",
                url: "checkcaptcha.php",
                data: "captcha=" + captchaCode,
                success: function (msg) {
                    if ($.trim(msg) == '2') {
                        alert('Entered code is wrong');
                        reloadCaptcha();
                        return false;
                    }
                    validated = true;<-- this will mark as valid.
                    $("#frmSignUp").submit();<-- and submit the page again. since this is validated now it will not prevent the submit.
                }
            });
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="frmSignUp" action="/account/signup?a=1" name="myForm" method="post">
    <table width="390" border="0" cellspacing="0" cellpadding="0" align="right">
        <td colspan="3">
            <div style="float: right;margin-right:26px;width:200px;">
                <img id="siimage" align="left" style="padding-right: 5px; border: 0" src="/securityimage/securimage_show.php?sid={/application/random}" />
                <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="19" height="19" id="SecurImage_as3" align="middle">
                    <param name="wmode" value="opaque" />
                    <param name="allowScriptAccess" value="sameDomain" />
                    <param name="allowFullScreen" value="false" />
                    <param name="movie" value="http://cheersoye.com/securityimage/securimage_play.swf?audio=/securityimage/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5" />
                    <param name="quality" value="high" />
                    <param name="bgcolor" value="#ffffff" />
                    <embed src="http://cheersoye.com/securityimage/securimage_play.swf?audio=http://cheersoye.com/securityimage/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5" quality="high" bgcolor="#ffffff" width="19" height="19" name="SecurImage_as3" wmode="opaque" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
                </object>
                <br/>
<a tabindex="-1" style="border-style: none" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = '/securityimage/securimage_show.php?sid=' + Math.random(); return false"><img src="<?php echo IMAGE_URL; ?>/refresh.gif" alt="Reload Image" border="0" onclick="this.blur()" align="bottom" /></a>

            </div>
        </td>
        </tr>
        <tr>
            <td class="log_label" colspan="2">Enter Code:</td>
            <td>
                <div id="" class="form-group">
                    <input name="confirmationcode" type="text" id="capcode" class="log_input" value="" />
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="3" style="text-align:center;">
                <input type="submit" value="Register" style="height:35px;" class="small_btn" />
            </td>
        </tr>
    </table>
</form>

答案 1 :(得分:0)

如果验证码为空或错误,请添加此行以防止提交表单。

<强> $(&#34;形式&#34)。提交(函数(E){         e.preventDefault();     });

答案 2 :(得分:0)

看看打击代码,如果是部分,则返回不会传递给其他人;

 if( document.myForm.capcode.value == "" ){
   alert( "Please type Code!" );
   document.myForm.capcode.focus() ;
   return false 
 }else if( document.myForm.capcode.value != "" ){
    var valid = true;
    $.ajax({
                type: "POST",
                url: "checkcaptcha.php",
                data: "captcha="+document.myForm.capcode.value,
                success: function(msg)
                    {
                    if($.trim(msg) == '2'){
                        alert('Entered code is wrong');
                        reloadCaptcha()
                        valid = false;
                    }
                }
            })
       return valid;
 }
相关问题