How to write username validation rules in Laravel?
天蓬老师
天蓬老师 2017-05-16 16:51:23
0
6
701

The requirement is that it must contain letters and numbers.

For example, pure numbers are not allowed.

How to implement it using laravel's own verification method?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(6)
巴扎黑

Just use this regular rule, it has been tested, [a-zA-Z]+([A-Za-z0-9])*

Achieve the effect, such as when registering a username:

55555 failed
8754xcc failed
hhgdG55 passed
hhgdG55? failed
ggh hhd failed
with Chinese failed

It seems that you can only use regular expressions, laravel does not provide corresponding function support (functions in the shape of alpha_num)

習慣沉默
//获取表单的值
$username = $request->input('username');
$password = $request->input('password');

//登录表单验证
$validator = Validator::make($request->all(), [
  'username' => 'required|alpha_num|regex:/^(?!([A-Za-z]+|d\d+)$)[A-Za-z\d]$/',  //只允许数字和字母
]);

//表单验证失败提示
if ($validator->fails()) {
  //此处省略。。。
}

Not tested.

某草草

You probably haven’t seen the Validation part of the Laravel5 document yet~ You can write a Request to verify the username

左手右手慢动作
 $form_data = [
            "product_name" => $request->get('product_name'),      
        ];
$rules = [
            "product_name" => 'required'
        ];
$messages = [
            'required' => ' :attribute 字段必须填写.',
        ];        
$validate = Validator::make($form_data, $rules,$messages);
if ($validate ->fails()){
    return redirect('admin/addProduct')->withErrors($validate)->withInput();
        }

I don’t know if I can help you

仅有的幸福

You need a laravel document: Baidu Cloud. After downloading the document, please check [Directory] [Service] [Service - Verification], or [Search] "Validator".

左手右手慢动作

Verification rules can be customized

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template