利用jquery.validationEngine ajax驗證後台id是否已存在
前台jquery validation架構驗證
jsp:
<link rel="stylesheet" type="text/css" href="<%=path%>/js/css/validationEngine.jquery.css"/><script type="text/javascript" src="<%=path%>/js/jquery/jquery-1.4.2.min.js"></script><script type="text/javascript" src="<%=path%>/js/validation/jquery.validationEngine-cn.js"></script><script type="text/javascript" src="<%=path%>/js/validation/jquery.validationEngine.js"></script> <input type="text" name="item.entry" id="item_entry" class="validate[required,custom[onlyNumber],ajax[ajaxItemNoCall],length[0,4]] text-input" size="10"/></td>
jquery.validationEngine-cn.js
"ajaxItemNoCall": { "url": "validateItemNo", "alertText": "* ID已被使用", "alertTextOk": "*ID可以使用", "alertTextLoad": "* 檢查中, 請稍候..." }
ItemAction.java
@Action(results = {@Result(type="json")}, params = { "excludeProperties", "validateReturn"}, value="validateItemNo")public String validateItemNo() throws Exception{ActionContext ct = ActionContext.getContext();HttpServletRequest request= (HttpServletRequest)ct.get(ServletActionContext. HTTP_REQUEST ); String validateId = request.getParameter("fieldId");String validateValue = request.getParameter("fieldValue");int valiValue = Integer.parseInt(validateValue); item = itemService.findItemById(valiValue);if(item.getEntry() != valiValue){validateReturn[0] = validateId;validateReturn[1] = true;validateReturn[2] = "可以使用";}else {validateReturn[0] = validateId;validateReturn[1] = false;validateReturn[2] = "在指定ID已經存在";}return "success";}