日期:2014-05-16  浏览次数:20825 次

jquery,Ajax判断验证码是否正确,不正确阻止提交

需要引入的js文件

<script type="text/javascript" src="__PUBLIC__/js/jquery.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery.form.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/artdialog/jquery.artDialog.js?skin={$Think.config.ART_DIALOG.skin}"></script>

js实现代码

<script type="text/javascript">
$(function(){
	//向客户端ajax发送数据
	$("#verify:text").blur(function(){
		var verify = $('#verify').val();
		var data = 'verify=' +verify;
		  $.ajax({  
			  type: "post", 
			  datatype:"json",  
			  url: "__URL__/verify",
			  data: data, 
			             success: function(data){
			            	if(data=='false'){
			            		$("form").submit(function(e){
			            		    e.preventDefault();
			            		  });
			            	}else{
			            		GetMoreSource()
			            		//document.getElementById('submit').submit()="false";
			            	}
		          	 },
			             error: function() { alert("error"); } , 
			      });  
	 });
});

function GetMoreSource() { 
	data=$('.calculate').serialize();
	  $.ajax({  
	  type: "post", 
	  datatype:"json",  
	  url: "__URL__/matchAdviserList",
	  data: data, 
	             success: function(data){
	            	$("#divok").html(data);
        	 },
	             error: function() { alert("error"); } , 
	          });  
}
php验证部分代码

 //判断验证码是否正确
    public function verify(){
        $verify=$this->_post('verify');
        if($_SESSION['verify']==md5($verify)&&!empty($verify)){
        	echo 'true';
        }else{
        	echo 'false';
        }
    }
html部分实现代码

<body>
<form action="" method="post" id="submitForAjax" >
aaa:<select name="webtype" class="calculate">
	<option value="" selected></option>
	
</select><br>
bbb :<input type="text" name="dayPv" class="calculate"><br>
ccc:<input type="text" name="indexPage" class="calculate"><br>
ddd:<input type="text" name="dataCon" class="calculate"><br>
eee:<input type="text" name="verify"  nullmsg="请输入验证码" id="verify"><span id="result"></span>
<a href="#"><img id="im"  src="__APP__/User/Login/verify" onclick="this.src='__APP__/User/Login/verify/?id='+Math.random()*5;" /></a>
<i><a href="#" title="换一张"  onclick="document.getElementById('im').src='__APP__/User/Login/verify/?id='+Math.random()*5;">换一张</a></i>
</form>
<input type="submit" value="按钮" id="submit" >


<div id="divok">
</div>


</body>
使用了异步验证