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
注:转自小白丶程序猿的博客
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年