jquery struts 驗證唯一標識(公用方法)

來源:互聯網
上載者:User

步驟一 引入js檔案
複製代碼 代碼如下:
<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>

步驟四 action裡的方法
複製代碼 代碼如下:
/**
*
* @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();
}
}

步驟五 dao裡的方法
複製代碼 代碼如下:
<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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.