最近有个项目用了 jQuery Validation Engine,发现在表单提交时,不验证ajax提交的项目,例如:验证码,就直接提交了。
解决如下例:
表单:vali.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=charset=GB18030">
<title>Insert title here</title>
<link rel="stylesheet" href="/cti_test/css/validationEngine.jquery.css"
type="text/css" />
<script src="/cti_test/js/jquery-1.7.js" type="text/javascript"></script>
<script src="/cti_test/js/jquery.validationEngine-zh_CN.js"
type="text/javascript"></script>
<script src="/cti_test/js/jquery.validationEngine.js" type="text/javascript"></script>
<script>
function ajaxValidationCallback(status, form, json, options){
if (console)
console.log(status);
if (status == true){
if (json[1] === true) {
alert("表单验证成功!");
}
else{
alert("表单验证失败!");
}
}
else{
alert("ajax验证程序错误");
}
}
jQuery(document).ready(function(){
jQuery("#form").validationEngine({
ajaxFormValidation: true,
onAjaxFormComplete: ajaxValidationCallback,
ajaxFormValidationURL : "ajax_test.jsp"
});
});
</script>
</head>
<body>
<br>
<br>
<br>
<br>
<form id="form" method="post" action="1.html">
<input type="text" name="code" id="code" class="validate[required, ajax[ajaxTest]]" />
<input type="text" name="user" id="user1" class="validate[required]"/>
<input type="submit" />
</form>
</body>
</html>
ajax校验程序:ajax_test.jsp
<%
String code = "";
String fieldValue = "";
String value = "";
code = request.getParameter("code") == null ? "" : request.getParameter("code");
fieldValue = request.getParameter("fieldValue") == null ? "" : request.getParameter("fieldValue");
if (!code.equals("")){
value = code;
}
else if (!fieldValue.equals("")){
value = fieldValue;
}
if (value.equals("")){
out.println("[\"code\",false]");
}
else if (value.equals("abc")){
out.println("[\"code\",true]");
}
else{
out.println("[\"code\",false]");
}
%>