Bootstrap表单校验(判断输入框中的值是否存在)

                     <form class="form-horizontal" id="sysUserInfo">
                        <!--隐藏区域-->
                        <input type="hidden" id="id_id" value="${mtUser.id!}">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="id_account" class="col-md-2 control-label">登录账号<span style="color: red">*</span></label>
                                <div class="col-md-2">
                                    <input type="text" <#if mtUser.account??>disabled="disabled"</#if> class="form-control" name="account" id="id_account" value="${mtUser.account!}">
                                </div>

                                <label for="id_password" class="col-md-2 control-label">登录密码
                                    <#if mtUser.id ??><#else><span style="color: red">*</span></#if>
                                </label>
                                <div class="col-md-2">
                                    <input type="password" class="form-control" autocomplete="new-password" name="password" id="id_password" >
                                </div>

                                <label for="id_password" class="col-md-2 control-label">状态
                                    <span style="color: red">*</span>
                                </label>
                                <div class="col-md-2">
                                    <select name="state" id="state">
                                        <option value="0">启用</option>
                                        <option value="1">禁用</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </form>
/**
         *  数据校验规则
         * */
        $('#sysUserInfo').bootstrapValidator({
            message: '这个数据是无效的!',
            group: '.rowGroup',
            feedbackIcons: {/*输入框不同状态,显示图片的样式*/
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                account: {
                    validators: {
                        notEmpty: {/*非空提示*/
                            message: '登录账号不能为空!'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_-]{6,20}$/,
                            message: '账号必须是数字或字母且6-20位'
                        },
                        remote: {
                            url: "/mtUser/accountValidators.do",
                            message: '用户名已存在,请重新输入',
                            delay: 1000,//ajax刷新的时间是1秒一次
                            type: 'POST',
                            data: function(validator) {
                                return {
                                    account : $("#id_account").val(),
                                    method : "checkAccount"
                                };
                            }
                        }
                    }
                },

                password: {
                    validators: {
                        notEmpty: {/*非空提示*/
                            message: '登录密码不能为空!'
                        },
                        regexp: {
                            regexp: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
                            message: '账号必须是数字和字母且6-20位'
                        }
                    }
                }
            }
        });
@RequestMapping(value = "/accountValidators", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject accountValidators(HttpServletRequest request, HttpServletResponse response) {
        try{
            JSONObject result = new JSONObject();
            String account = request.getParameter("account");
            boolean flag = mtUserService.queryMtUserInfoByAccount(account);
            if (flag){
                result.put("valid", false);
            }else {
                result.put("valid", true);
            }
            return result;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值