H-UI的前端处理验证,判断是否已经存在,比较健全的模板,可以自己添加一些校验

<input type="text" class="input-text" value="${detail.supportingname }" placeholder="" id="S-name" name="name"  datatype="usercheck" nullmsg="服务名不能为空" errormsg="格式不正确,请重新输入!">

$(function(){
  $("#form-admin-add").Validform({
    tiptype:2,
    btnSubmit:"#btn_sub",
    datatype:{
      "headimg":function(gets,obj,curform,regxp){
        //参数gets是获取到的表单元素值,obj为当前表单元素,curform为当前验证的表单,regxp为内置的一些正则表达式的引用;
        if(!$("input[name='headimg']").val())
          return "必须上传图片!";
        else{
          return true;
        }
      },
      "usercheck":function(gets,obj,curform,regxp){
        var supportingnamesx=$("#supportingnamesx").val();
        var name=obj.val();
        if(name==null){
          return "服务名称不能为空";
        }else{
        if (name.length>16||name.length<2){
          return "请填写2到16位任意字符!";
        }else{
        var flag=false; //必须通过定义,在后面的传递错误信息,不然会不执行
        $.ajax({
          type:"post",
          async: false,
          url:"/admin/SupportingService/Update/isName",
          data:'name='+name+'&supportingnamesx='+supportingnamesx,
          success:function(data){
            var obj=eval(data);
            obj=eval(obj);
            if(obj[0]['isok'] ){
              flag = true;

            }else{
              flag = false;
            }
        }
        });
        //仔细看哦!
        if(flag==true){
          return "配套服务名已存在,请重新输入!";
        }else{
          return true;
        }

        }
        }
      },
      "checknumber":function(gets,obj,curform,regxp){
        var jiunumber=$("#jiunumber").val();
        var number=obj.val();
        if(number==null){
          return "排序号不能为空!";
        }else{
        /* var strRegex ="(/^(\+|-)?\d+$/.test( value ))&&value>0";
          var re=new RegExp(strRegex); */
          if ((/^(\+|-)?\d+$/.test( number ))&&number>0)
            {
            var flag=false;
          $.ajax({
            type:"post",
            async: false,
            url:"/admin/SupportingService/Update/isNumber",
            data:'number='+number+'&jiunumber='+jiunumber,
            success:function(data){
              var obj=eval(data);
              obj=eval(obj);
              if(obj[0]['isok'] ){
                flag = true;

              }else{
                flag = false;
              }
            }
            });
          if(flag==true){
            return "排序号已存在,请重新输入!";
          }else{
            return true;
          }

         }else{
          return "请输入整数哦!";
      }
    }

    }
    },

    beforeSubmit:function(curform){
      var selected = "";
      var inputs=document.getElementsByName("isvalid1");
      var oshow=document.getElementById("show");

      //获取选择器中的值  

      for(var index=0;index<inputs.length;index++){
        if(inputs[index].checked==true){
        selected=inputs[index].value;
        }
      }
      ajaxSubmit("/admin/SupportingService/Update?isvalid="+selected,$('#form-admin-add'), function(msg){
        var obj=eval(msg);
        obj=eval(obj);
        var number=$("#number").val();
        var name=$("#S-name").val();
          if(obj[0]["isok"]){
            parent.layer.msg('保存成功!<script>setTimeout("window.location.reload();",1100);<\/script>', {icon: 6,time:1150});
            var index = parent.layer.getFrameIndex(window.name);
            parent.$('.btn-refresh').click();
            parent.layer.close(index);
          }else{

            parent.layer.msg('添加失败,请重试!',{icon: 5,time:3000});
      }
      });
    }
  });
});

posted @ 2015-12-14 11:13  柠檬真酸  阅读(3353)  评论(0编辑  收藏  举报