CI好用的ajax验证

//js下载地址 http://yl96.cn/statics/js/jquery.validate.js

$(function(){
    $("#formID").validate({  //表单id
        rules:{
            username:{
                required: true,
                minlength:2,
                maxlength:20,
                remote:{                                          //验证用户名是否存在
                    type : "post",
                    url  : "<?php echo site_url('init/public_checkname_ajax')?>",
                    data:{
                        username:function(){
                            return $("#username").val();
                        },
                        rand:function(){
                            return Math.random();
                        }
                    }
                }
            },
            password:{
                required: true,
                minlength:6,
                maxlength:20
            },
            pwdconfirm:{
                required: true,
                minlength:6,
                maxlength:20,
                equalTo:"#password"
            },
            email:{
                required:true,
                email: true,
                remote:{                                          //验证邮箱是否存在
                    type : "post",
                    url  : "<?php echo site_url('init/public_checkemail_ajax')?>",
                    data:{
                        email:function(){
                            return $("#email").val();
                        },
                        rand:function(){
                            return Math.random();
                        }
                    }
                }
            },
            phone:{
                required:true,
                phone: true,
                remote:{                                          //验证手机是否存在
                    type : "post",
                    url  : "<?php echo site_url('init/public_checkphone_ajax')?>",
                    data:{
                        phone:function(){
                            return $("#phone").val();
                        },
                        rand:function(){
                            return Math.random();
                        }
                    }
                }
            },
            hospital:{
                required: true,
                minlength:2
            },
            subject:{
                required: true,
                minlength:2
            }
        },
        messages: {
            username: {
                required: "请输入用户名",
                minlength: jQuery.format("用户名不能小于{0}个字符"),
                remote:jQuery.format("用户名已经被注册")
            },
            password:{
                required: "请输入密码",
                minlength: jQuery.format("密码不能小于{0}个字符")
            },
            pwdconfirm:{
                required: "请输入确认密码",
                minlength: "确认密码不能小于{0}个字符",
                equalTo: "两次输入密码不一致不一致"
            },
            email:{
                required: "请输入Email地址",
                email: "请输入正确的email地址",
                remote:jQuery.format("邮箱已经被注册")
            },
            phone:{
                required: "请输入手机号",
                phone: "请输入正确的手机号",
                remote:jQuery.format("手机号已经被注册")
            },
            hospital:{
                required: "请输入医院",
                minlength: jQuery.format("请输入医院")
            },
            subject:{
                required: "请输入科室",
                minlength: jQuery.format("请输入科室")
            }
        },
        
        errorClass: "help-inline",
        errorElement: "span",
        highlight:function(element, errorClass, validClass) {
            $(element).parents('.control-group').removeClass('success');
            $(element).parents('.control-group').addClass('error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.control-group').removeClass('error');
            $(element).parents('.control-group').removeClass('success');
        },
        onfocusout: function(element) { $(element).valid(); }
    });
})



转载于:https://my.oschina.net/newrex/blog/649681

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值