• jQuery Validate 插件验证,,返回不同信息(json remote)自定义


    解题思路:输入的登陆信息远程验证是否该账号已存在,并提示相对应的信息

    $(document).ready(function() {
    // 在键盘按下并释放及提交后验证提交表单
      $("#signupForm").validate({
        rules: {
          username:{
                required: true,
                 remote   : {
                        url : '#index.php?check_suser_name_exist',
                        type: 'get',
                        data:{
                            username : function(){
                                return $('#username').val();
                            },
                            supplier_id: function(){
                                return     $("#select_supplier_id").val();
                            }
                        }
                    }
                },
          password: {
            required: true,
            minlength: 5
          },
          confirm_password: {
            required: true,
            minlength: 5,
            equalTo: "#password"
          },
        },
        messages: {
         supplier_username:{
                    required: '<i class="icon-exclamation-sign"></i>请填写登录账号',
                    remote     : '<i class="icon-exclamation-sign"></i>登录账号已经被占用,请换一个'
                },
       
          password: {
            required: "请输入密码",
            minlength: "密码长度不能小于 5 个字母"
          },
          confirm_password: {
            required: "请输入密码",
            minlength: "密码长度不能小于 5 个字母",
            equalTo: "两次密码输入不一致"
          },
          
    });
    
    

    remote(url):请求远程校验。url 通常是一个远程调用方法。

        public function check_suser_name_existOp() {
            $condition = array();
            $condition['suser_name'] = $_GET['username'];
            $condition['supplier_id'] = intval($_GET['supplier_id']);//本供应商唯一 
    
            $model_supplier_user = Model('supplier');
            $result = $model_supplier_user->getSupplierUserInfo($condition,'suser_id');
            if(!$result) {
                echo 'true';
            } else {
                echo 'false';
            } 
        }
    
    

    延伸:输入的登陆信息远程验证是否该账号已存在,但remote 返回true或false,但如果需要存在第三种情况,例如已经注册但还没启用??

    添加自定义效验

    addMethod:name, method, message

    supplier_username:{             
                    required:true,
                    check_suser_name: true
                    
                },
    //s make-in-lemon 账户存在重复处理    
    jQuery.validator.methods.check_suser_name = function(value, element) {
        var result = true;
        $.ajax({
            type:"GET",
            url:'index.php?act=supplier_joinin&op=check_user_name_exist',
            data:{
                        username : function(){
                            return $('#username').val();
                        },
                        supplier_id: function(){
                            return     $("select[name='supplier_id']").val();
                        }
                    },
            dataType: 'json',
            success:function(data){    
                     if(data == 'already'){    
                         alert('登录账号已经被占用,请换一个');    
                         $('#username').val('');
                     
                         
                     }
                     if(data == 'close'){
                       alert('此账号在审核中,请直接登录查看'); 
                       $('#username').val('');
                       $('#username_tips').css('display','');
             }
                     if(data == 'success'){
                        return $('#username').val();
                     }
                    } 
                      
        });
        return result;
    };
    //e make-in-lemon 账户存在重复处

    check_suser_name 自定义

    由  check.php 获取 返回值 1.already 2.close 3.success
    提示相应的提示

      public function check_user_name_existOp() {
            $condition = array();
            $condition['suser_name'] = $_GET['username'];
            $condition['supplier_id'] = intval($_GET['supplier_id']);//本供应商唯一 
    
            $model_supplier_user = Model('supplier');
            $result = $model_supplier_user->getSupplierUserInfo($condition,'suser_id');
    		$supplier_user_exit = $model_supplier_user->getSupplierUserInfo($condition);
    		if($supplier_user_exit['suser_state'] == 1) {
                        echo json_encode('already');
                    } elseif(  $supplier_user_exit['suser_state'] == "0" ||  $supplier_user_exit['suser_state'] == 2) {
                        echo json_encode('close');
                    }else{
    					 echo json_encode('success');
    				}

    相关链接

    JQuery的详细教程

    JQuery中$.ajax()方法参数详解

    jquery-validate 的remote验证

    jquery下的json格式ajax实现

  • 相关阅读:
    一篇文章搞清除异地多活 lcl
    设计业务异地多活架构 lcl
    Druid源码解析(八):DruidDataSource的recycle过程 lcl
    微服务架构&中台架构落地技巧 lcl
    H5如何实现唤起APP
    联盟链 Hyperledger Fabric 应用场景
    Hyperledger Fabric 核心概念
    Java中如何快捷的创建不可变集合
    Spring Boot 3.0.0 M3、2.7.0发布,2.5.x将停止维护
    Stream.toList()和Collectors.toList()的性能比较
  • 原文地址:https://www.cnblogs.com/lemonphp/p/5336632.html
Copyright © 2020-2023  润新知