easyui 表单和自定义验证扩展和js自定义返回值

时间:2024-07-09 12:33:26

================jsp==========================
<form  method="post" id="regfrminp" class="regfrminp">
 <div>
      <ul class="regtitle" >
       <li>公司名称:
        <input type="text" class="easyui-validatebox" required="true" name="aname" id="uname"/>
       </li>
       <li>公司帐号:<input type="text" name="acode" id="acode"  class="easyui-validatebox" validtype="remote['index/checkput.hml','acode','该公司帐号已注册']"  required="true"/>
       </li>
       <li>常用邮箱:<input type="text" name="email" id="email"  class="easyui-validatebox"  validtype="email['index/checkput.hml','email','邮箱格式不正确','添加']"   required="true" />
       </li>
       <li>用户密码:<input type="password" name="pass"  class="easyui-validatebox" data-options="required:true" id="pwd" />
       </li>
       <li>确认密码:<input type="password" class="easyui-validatebox" data-options="required:true" validType="equalsTo['pwd','密码不一致']" name="passt" id="pwdt" onkeyup="keyUpEnter()"/>
       </li>
       <li id="regbtn" >
        <a href="javascript:void(0)">
         <img alt="" src="data:images/yes.png" onclick="easyuiSubFrom('PubaccinfoController/reg.hml','regfrminp')"  class="btnstyle"/>&nbsp;
        </a>
        <a href="">
         <img alt="" src="data:images/back.png" class="btnstyle"/>
        </a>
       </li>
      </ul>
     </div>
</form>

==================js==================================
// easyui 非ajax提交表单
function easyuiSubFrom(url, frmId) {
 if (frmId.substring(0, 1) != "#") {
  frmId = "#" + frmId;
 }
 if ($(frmId).form('validate')) {
  $(frmId).attr("method", "post");
  $(frmId).attr("action", url);
  $(frmId).submit();
 }
}
// easyui ajax提交表单
function easyuiAjaxSubFrom(url, frmId) {
 if (frmId.substring(0, 1) != "#") {
  frmId = "#" + frmId;
 }
 var flag = false;
 $(frmId).form('submit', {
  async : false,
  url : url,
  data : $(frmId).serialize(),
  onSubmit : function() {
   return $(frmId).form('validate');// 对数据进行格式化
  },
  success : function(data) {
   flag = true;
  },
  error : function(msg) {
   flag = false;
  }
 });
 return {
  result : flag
 };
}

// easyui 通用的验证扩展
$.extend($.fn.validatebox.defaults.rules,{
     remote : {// 帐号验证
      validator : function(value, url) {
       var returnFlag = true;
       $.ajax({
        async : false,
        type : "POST",
        url : url[0],
        data : url[1] + "=" + value,
        success : function(msg) {
         var jsonObj = eval(+""("+msg+")"");
         if (jsonObj.id != "0") {
          returnFlag = false;
          url[2] = jsonObj.text;
         } else {
          returnFlag = true;
         }
        },
        error : function() {
         url[2] = "抱歉,服务器验证信息失败!";
         returnFlag = false;
        }
       });
       return returnFlag;

},
      message : "{2}"
     },
     email : {// 邮箱验证
      validator : function(value, url) {
       var checkMail = /^([a-z0-9]|[A-Z0-9])+@+([a-z0-9]|[A-Z0-9])+\.+com$/;
       if (!checkMail.test($.trim(value))) {
        return false;
       }
       if (url[3] == '修改') {
        if (value == $("#" + url[4]).val()) {//修改邮箱,排除自己当前的邮箱,其它的邮箱如果存在,该邮箱已经存在
         returnFlag = true;
         return returnFlag;
        }
       }

var returnFlag = true;
       $.ajax({
        async : false,
        type : "POST",
        url : url[0],
        data : url[1] + "=" + value,
        success : function(msg) {
         var jsonObj =eval(+""("+msg+")"");
         if (jsonObj.id != "0") {
          returnFlag = false;
          url[2] = jsonObj.text;
         } else {
          returnFlag = true;
         }
        },
        error : function() {
         url[2] = "抱歉,服务器验证信息失败!";
         returnFlag = false;
        }
       });
       return returnFlag;

},
      message : "{2}"
     },
     equalsTo : {// 验证第二次输入是否与第一次输入一致
      validator : function(value, oldValue) {
       var returnFlag = true;
       if (value != $("#" + oldValue).val()) {
        returnFlag = false;
       } else {
        returnFlag = true;
       }
       return returnFlag;
      },
      message : "{1}"
     },
     nulls : {// 输入空格验证
      validator : function(value) {
       if ($.trim(value) != "") {
        return value;
       }
      },
      message : "{0}"
     },
     mobile : {// 手机验证
      validator : function(value) {
       var reg = /^1[3|4|5|8|9]\d{9}$/;
       return reg.test(value);
      },
      message : "{0}"
     },
     phone : {// 电话验证
      validator : function(value) {

var reg = /^(\d{3,4}-)?\d{7,9}$/;
       return reg.test(value);
      },
      message : "{0}"
     }
    });

.............测试.........................
function subReg1(){
 var flag=false;
 $('#regfrminp').form('submit', {
  async : false,
  url : 'PubaccinfoControllers/reg.hml',
  data : $('#regfrminp').serialize(),//序列化表单
  onSubmit : function() {
   return $('#regfrminp').form('validate');// 对表单数据进行验证
  },
  success : function(data) {
   flag=true;
  },
  error : function(msg) {
   flag=false;
  }
 });
 return{result:flag};//返回值
};
function subReg() {
 alert(subReg1().result);//测试返回值
}