jquery struts 验证唯一标识(公用方法)
<script language="javascript1.2" src="<s:url value="/js/jquery.js" includeParams="false"/>"></script>
<script language="javascript1.2" src="<s:url value="/js/dictionary.js" includeParams="false"/>"></script>
步骤二 公用js文件 dictionary.js
代码如下:
/**
*↓↓↓↓↓↓↓↓↓↓
*作者:ZhuangZi
*类名:公用方法初始化
*功能:公用方法初始化
*详细:公用方法初始化
*版本:1.0
*日期:2013-03-22
*说明:
*↑↑↑↑↑↑↑↑↑↑
*/
$(function(){
/*1.失去焦点验证↓↓↓*/
$("#code").blur(callback);//1.code就是你要验证的唯一标志的控件ID 2.你控件后面必须要有个接收错误信息的控件
/*2.提交时候验证↓↓↓*/
$("#submit").click(callback);//你页面的提交按钮ID
});
/**
*↓↓↓↓↓↓↓↓↓↓
*作者:ZhuangZi
*类名:验证唯一标识公用方法
*功能:唯一验证
*详细:唯一标识ID和提交按钮ID要和下面保持一致
*版本:1.0
*日期:2013-03-22
*说明:
*↑↑↑↑↑↑↑↑↑↑
*/
function callback() {
var slef=$("#code");
var code=slef.val();
var submit=$("#submit");
var flag=$("#flag").val();
if(null==code||""==code){
slef.next().html("下载包标识不能为空!");
submit.attr("disabled",true);
return;
}else{
if(!code.match("^\\w+$")){
slef.next().html("标识由数字母或下划线组成!");
return ;
}
slef.next().html("*");
}
$.ajax({
type:'post',
url:'../dictionary/checkCodeOnly.do',
data:'bean.code='+code+'&bean.flag='+flag,
dataType:'json',
success:function(json){
if(json>0){
slef.next().html("此标识已存在!");
submit.attr("disabled",true);
return ;
}else{
slef.next().html("");
if(code != null && code!=""){
submit.attr("disabled",false);
}else{
submit.attr("disabled",true);
return;
}
}
},
error:function(){
alert('验证信息出错');
}
});
}
<s:hidden id="flag" value="3"/> <!-flag 什么类型的标志->
<tr>
<td width="40%" height="33" class="addtabletd1">下载标识:</td>
<td width="60%" height="33" class="addtabletd2"><div align="left">
<s:textfield id="code" name="yhaoPortalsDownBean.downFlag" cssClass="textfrom" cssStyle="width:150px;" maxlength="200"/>
<font id="codeInfo" color="red">*</font>
</div></td>
</tr>
/**
*
* @author ZhuangZi
* @class com.hzdracom.action.YhaoPortalsDictionaryAction
* @method checkCodeOnly
* @Directions 验证唯一标识公用方法
* @date 2013-3-21上午10:09:04 void
*/
public void checkCodeOnly(){
String json="";
try{
json = String.valueOf(yhaoDictionaryService.checkCodeOnly(bean));
json=JSON.toJSONString(json);
System.out.println("json=="+json);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out;
out = response.getWriter();
out.println(json);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
<PRE class=java name="code"> public int checkCodeOnly(DictionaryBean bean)throws DataAccessException,Exception {
Object[] sqlParams = new Object[4];
int index = 0;
String sql="";
/* 验证页面标志是否唯一*/
if (bean.getFlag().equals("1")) {
sql = "select count(1) from YHAO_PORTALS_PAGE where PAGE_FLAG = ? ";
sqlParams[index]=bean.getCode();
index++;
}
/* 验证类型志标识是否唯一*/
if (bean.getFlag().equals("2")) {
sql = "select count(1) from YHAO_PORTALS_DOWNTYPE where DOWNTYPE_FLAG = ? ";
sqlParams[index]=bean.getCode();
index++;
}
/* 验证下载志标识是否唯一*/
if (bean.getFlag().equals("3")) {
sql = "select count(1) from YHAO_PORTALS_DOWN where DOWN_FLAG = ? ";
sqlParams[index]=bean.getCode();
index++;
}
if (bean.getFlag().equals("4")) {
sql = "select count(1) from YHAO_PORTALS_KUAI where KUAI_FLAG = ? ";
sqlParams[index]=bean.getCode();
index++;
}
Object[] sqlParamsEnd = new Object[index];
System.arraycopy(sqlParams, 0, sqlParamsEnd, 0, index);
int count = this.queryForInt(sql,sqlParamsEnd);
return count;
}</PRE>
<PRE></PRE>