表单验证

来源:互联网 发布:好听有内涵的名字知乎 编辑:程序博客网 时间:2024/04/26 05:34

验证表单
//JS部分

function makeValidate() {    validator = $("#form").validate({        meta: "validate",        onfocusout: function(element) {            this.element(element);        },        errorPlacement: function(error, element) {            if (element.is("input") || element.is("select")) {//验证<input>或<select>                element.addClass("inErr");                if (element.next().is("div")) {                    element.next().removeClass("ico-ok");                    element.next().addClass("ico-ng");                    element.next().empty();                    error.appendTo(element.next());                } else {                    element.next().next().removeClass("ico-ok");                    element.next().next().addClass("ico-ng");                    element.next().next().empty();                    error.appendTo(element.next().next());                }            }        },        errorElement: "div",        success: function(label) {            if (label.parent().prev().is("input")) {               label.parent().prev().removeClass("inErr");            } else {               label.parent().prev().prev().removeClass("inErr");            }            label.parent().removeClass("ico-ng");        label.parent().html("&nbsp;&nbsp;&nbsp;").addClass("ico-ok");        },        rules : {            "weixin" : {//验证的ID            required: true,            isChar: true,            minlength: 6,            maxlength: 16,            isExistWeixin: true            },        },        messages: {            "weixin": {                 required: "必须输入",                 isChar: "傻了吧",                 maxlength: "太长了",                 minlength: "too short",                 isExistWeixin: "重复了"            }        }    });/*------------------------------------------    验证 修改后是否重复------------------------------------------*/    $.validator.addMethod(        "isExistWeixin",//验证微信名是否重复        function(value, element) {            var flg = true;            var nowWx = $("#weixin").val();            if(nowWx != "" && nowWx.length >= 6){                $.ajax({                    url: "index.php?m=admin&c=index&a=checkWx",                    data: "name=" + $("#weixin").val() + "&wxId=" + $("#wxId").val(),                    type: "GET",                    async : false,                    cache: false,                    success : function(data, textStatus) {                    if(data == "0"){                        flg = false;                    }                },                error : function(data, textStatus) {                    flg = false;                }                });            }            return flg;        }    );}

html部分

<form action="index.php?m=admin&c=index&a=up&id={$brrList[0][id]}" method='post' enctype="multipart/form-data" id="form" name="form" class="cmxform">    <div class="form-group">    <input type="hidden" id="wxId" value={$brrList[0][id]}>    <label>微信公众号</label>    <input id="weixin" class="form-control" name="weixin" value={$brrList[0][weixin]}><div></div>    <button class="btn btn-success" type="submit">修改</button>    <a href="index.php?m=admin&c=index&a=data&page=1"><button class="btn btn-default" type="button">返回</button></a></form>

php部分

public  function checkWx(){        $user = M('weixin_user');        $parm = $_GET['name'];        if ($_GET['wxId']){            $wxId = $_GET['wxId'];        }        $where['id'] = array("NEQ","$wxId");        $where['weixin'] = array("EQ","$parm");        $rs = $user->where($where)->select();        if($rs){            echo 0;            exit;        }        echo 1;    }
1 0