zoukankan      html  css  js  c++  java
  • laravel表单验证

    效果展示:

           

    代码 实现:

    后台:

    use Validator;

    public function login()  
        {  
            if($input = Input::all()){  
                //验证提交的数据  
                $rules = [  
                    'user_name'=>'required|between:4,20',  
                    'user_pwd'=>'required|between:6,20',  
                    'code'=>'required|between:4,4'  
                ];  
                $message = [  
                    'user_name.required'=>'账号不能为空!',  
                    'user_name.between'=>'账号必须在6-20位之间!',  
                    'user_pwd.required'=>'密码不能为空!',  
                    'user_pwd.between'=>'密码必须在6-20位之间!',  
                    'code.required'=>'验证码不能为空!',  
                    'code.between'=>'验证码必须为4位!',  
                ];  
                $validator = Validator::make($input,$rules,$message);  
                //表单验证  
                if($validator->passes()){  
                //验证验证码  
                if($_SESSION['code']!=strtoupper($input['code'])){  
                    return back()->with('msg','验证码错误!');  
                }  
                //用户验证  
                $user = DB::table('user')->select  
      
    ('user_id','user_encrypt','user_pwd')->where('user_name',$input['user_name'])-  
      
    >first();  
                if(!$user){  
                    return back()->with('msg','用户不存在!');  
                }else{  
                    if(md5(md5($input['user_pwd']).$user->user_encrypt)!=$user-  
      
    >user_pwd){  
                        return back()->with('msg','密码错误!');  
                    }  
                    session(['user_id'=>$user->user_id]);  
                    return redirect('index');  
                   }  
                }else{  
                    return back()->withErrors($validator);  
                }  
            }else{  
                return view('admin.login');  
            }  
      
        }  

    前台:

    <div class="middle-box text-center loginscreen  animated fadeInDown">  
            <div>  
                <div>  
      
                    <h1 class="logo-name">h</h1>  
      
                </div>  
                <h3>欢迎使用 hAdmin</h3>  
                  
                <form class="m-t" role="form" action="{{URL('/login')}}" method="post">  
                   @if(count($errors)>0)  
                                @if(is_object($errors))  
                                    @foreach($errors->all() as $error)  
                                    <p style="color:red">{{$error}}</p>  
                                    @endforeach  
                                @else  
                                <p style="color:red">{{$errors}}</p>  
                            @endif  
                        @endif  
                        @if(session('msg'))  
                            <p style="color:red">{{session('msg')}}</p>  
                        @endif  
                    <div class="form-group">  
                        <input type="email" class="form-control" placeholder="用户名" required="" name="email">  
                    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">  
                    </div>  
                    <div class="form-group">  
                        <input type="password" class="form-control" placeholder="密码" required="" name="pwd">  
                    </div>  
                    <button type="submit" class="btn btn-primary block full-width m-b">登 录</button>  
      
      
                    <p class="text-muted text-center"> <a href="login.html#"><small>忘记密码了?</small></a> | <a href="{{URL('/register')}}">注册一个新账号</a>  
                    </p>  
      
                </form>  
            </div>  
        </div>  

    $validator->fails()  验证都符合规则返回false 不符合规则返回true

    注:转自小白丶程序猿的博客

  • 相关阅读:
    idea 配置 scala
    Error contacting service. It is probably not running.
    ipc.Client: Retrying connect to server: .../10.0.0.27:10020. Already tried 6 time(s); retry policy is RetryUpToMaximumCountWithFixedSleep(maxRetries=10, sleepTime=1 SECONDS)
    hadoop安装配置
    WordCount-JAVA版
    scp、rsync、xsync
    我所经历的开题
    无情的岁月之流水一般的一年级
    数据挖掘中易犯的几大错误【转载,侵删】
    如何摧毁程序员的效率?【转载,侵删】
  • 原文地址:https://www.cnblogs.com/zmdComeOn/p/10250724.html
Copyright © 2011-2022 走看看