jquery.validationEngine ajax表单验证数据库重复,指定数据库表名(tableName)、表字段(columnName)和录入数据(inputValue)进行验证,sql为 select count(*) from tableNamewhere columnName = inputValue,判断count并返回。
需要修改jquery.validationEngine.js和jquery.validationEngine-zh_CN.js文件来完成。
/** * Ajax field validation * * @param {jqObject} field * @param {Array[String]} rules * @param {int} i rules index * @param {Map} * user options * @return nothing! the ajax validator handles the prompts itself */ _ajax: function(field, rules, i, options) { var errorSelector = rules[i + 1]; /** * 增加参数 表名和字段名 */ var tableName = ""; var columnName = ""; if( rules[i + 2]) tableName = rules[i + 2]; if( rules[i + 3]) columnName = rules[i + 3]; var rule = options.allrules[errorSelector]; var extraData = rule.extraData; var extraDataDynamic = rule.extraDataDynamic; var data = { "fieldId" : field.attr("id"), "fieldValue" : field.val(), "tableName" : tableName, "columnName" : columnName }; if (typeof extraData === "object") { $.extend(data, extraData); } else if (typeof extraData === "string") { var tempData = extraData.split("&"); for(var i = 0; i < tempData.length; i++) { var values = tempData[i].split("="); if (values[0] && values[0]) { data[values[0]] = values[1]; } } }
//tls warning:homegrown not fielded "dateTimeFormat": { "regex": /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])\s+(1[012]|0?[1-9]){1}:(0?[1-5]|[0-6][0-9]){1}:(0?[0-6]|[0-6][0-9]){1}\s+(am|pm|AM|PM){1}$|^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^((1[012]|0?[1-9]){1}\/(0?[1-9]|[12][0-9]|3[01]){1}\/\d{2,4}\s+(1[012]|0?[1-9]){1}:(0?[1-5]|[0-6][0-9]){1}:(0?[0-6]|[0-6][0-9]){1}\s+(am|pm|AM|PM){1})$/, "alertText": "* 无效的日期或时间格式", "alertText2": "可接受的格式: ", "alertText3": "mm/dd/yyyy hh:mm:ss AM|PM 或 ", "alertText4": "yyyy-mm-dd hh:mm:ss AM|PM" }, //通用表单验证 "checkIsExist" : { "url" : "Common!checkIsExist.action", "extraData" : "", "alertText" : "* 输入值已被使用", "alertTextLoad" : "* 正在确认输入值是否被使用,请稍等。" }
<%@ page contentType="text/html; charset=UTF-8"%> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="./jquery/css/validationEngine.jquery.css" /> <script type="text/javascript" src="./jquery/jquery-1.9.1.js"></script> <script type="text/javascript" src="./jquery/jquery.validationEngine.js"></script> <script type="text/javascript" src="./jquery/jquery.validationEngine-zh_CN.js"></script> <script type="text/javascript"> function insertTest() { $("#myform").submit(); } function listTest() { var myform = document.forms[0]; myform.action = "Test!list.action"; myform.method = "post"; myform.submit(); } function init(){ $("#myform").validationEngine({ // 属性promptPosition : "bottomLeft" }); } </script> </head> <body onload="init();"> <h1>样例</h1> <hr> <form name="myform" id="myform" action="Test!save.action" method="post"> <!--对ID进行重复验证--> id: <input type="text" name="test.id" class="validate[required,ajax[checkIsExist,t_test,id]]"> <br> <br> name: <input type="text" name="test.name" class="validate[required,minSize[1],maxSize[10]]" > <br> <input type="button" name="btninsert" onclick="insertTest()" value="增加" /> <input type="button" name="btninsert" onclick="listTest()" value="列表页面" /> </form> </body> </html>
package com.zhou.bean; /** * @author zhouzhenlong * @date 2014-1-29 上午11:02:40 * @description * @version V1.0 */ public class Common { /** * tableName 表名 */ private String tableName; /** * @param columnName * 列名 */ private String columnName; /** * @param columnValue * 列值 */ private String columnValue; public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public String getColumnName() { return columnName; } public void setColumnName(String columnName) { this.columnName = columnName; } public String getColumnValue() { return columnValue; } public void setColumnValue(String columnValue) { this.columnValue = columnValue; } }
package com.zhou.action; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.zhou.bean.Common; import com.zhou.services.CommonService; /** * @author zhouzhenlong * @date 2014-1-29 上午11:00:14 * @description * @version V1.0 */ public class CommonAction { private static final long serialVersionUID = 1L; private CommonService commonService; /** * 公用方法,根据表名 列名 和值检查数据库是否存在该信息 */ public void checkIsExist() { PrintWriter pw = null; try { ActionContext ac = ActionContext.getContext(); HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST); HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE); String fieldId = request.getParameter("fieldId"); String tableName = request.getParameter("tableName"); String columnName = request.getParameter("columnName"); String fieldValue = request.getParameter("fieldValue"); Common common = new Common(); common.setTableName(tableName); common.setColumnName(columnName); common.setColumnValue(fieldValue); int count = commonService.checkIsExist(common); pw = response.getWriter(); if (count == 0) { pw.write("[\"" + fieldId + "\"," + true + "]"); } else pw.write("[\"" + fieldId + "\"," + false + "]"); } catch (Exception e) { e.printStackTrace(); } finally { pw.flush(); pw.close(); } } public void setCommonService(CommonService commonService) { this.commonService = commonService; } }
package com.zhou.services; import com.zhou.bean.Common; /** * @author zhouzhenlong * @date 2014-1-29 上午11:05:40 * @description * @version V1.0 */ public interface CommonService { /** * 验证接口 * @param common * @return */ public int checkIsExist(Common common); }
package com.zhou.services.impl; import com.zhou.bean.Common; import com.zhou.dao.CommonDao; import com.zhou.services.CommonService; /** * @author zhouzhenlong * @date 2014-1-29 上午11:08:37 * @description * @version V1.0 */ public class CommonServiceImpl implements CommonService { private CommonDao commonDao; @Override public int checkIsExist(Common common) { // TODO Auto-generated method stub return commonDao.checkIsExist(common); } public void setCommonDao(CommonDao commonDao) { this.commonDao = commonDao; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhou.dao.CommonDao"> <select id="checkIsExist" parameterType="com.zhou.bean.Common" resultType="int"> select count(*) from ${tableName} where ${columnName} = #{columnValue} </select> </mapper>
<!-- ajax通用验证 -->
<bean id="commonAction" class="com.zhou.action.CommonAction"
scope="prototype">
<property name="commonService" ref="commonService" />
</bean>
<bean id="commonDao" class="org.mybatis.spring.mapper.MapperFactoryBean"
scope="prototype">
<property name="mapperInterface" value="com.zhou.dao.CommonDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="commonService" class="com.zhou.services.impl.CommonServiceImpl"
scope="prototype">
<property name="commonDao" ref="commonDao" />
</bean>
<action name="Common!*" class="commonAction" method="{1}"> </action>