1. 程式人生 > >jQuery表單驗證以及ajaxForm

jQuery表單驗證以及ajaxForm

$(document).ready(function(){
  //表單驗證
  $("#projForm").validate({
rules: {
fullName:{  
                required:true,  
                remote:{  
                    type:"post",//資料請求方式  
                    url: "${pageContext.request.contextPath}/sysprojbid/xxx.do?projId=${projId}",//非同步驗證路徑  
                    data:{  
                        fullName:function(){  
                            return $("#fullName").val();  
                        },
                    },  
                    dataType:"html",  
                    dataFilter:function(data,type){//AJAX非同步返回資料 
                        var flag = eval("("+data+")");  
                        if(flag){  
                            return true;  
                        }else{  
                            return false;  
                        }  
                    }  
                }  
            } ,
projCode
:{required:true, remote:"${pageContext.request.contextPath}/sysprojbid/projcodeunique.do?projId=${proj.projId}"},
remark: {maxlength:200}
},messages: {
fullName: {remote:"專案全稱已經存在!"},
projCode: {remote:"專案編號已經存在!"}
}
});
 
  //ajax提交
      $("#projForm").ajaxForm({
          dataType:"json", 
          success:function (data){
            if(data.state == "success"){
                $.messager.alert("提示", "儲存成功", "info",function(){
                try {
                opener.searchResource();
                opener.searchSub();
                } catch (e) {} //重新整理dataGrid頁面
       window.close();
           });
            }else{
                $.messager.alert("錯誤", "儲存失敗", "error");  
            }
          }
      });
  });