Viewed   397 times

How to added password validation rule in the validator?

Validation rule:

The password contains characters from at least three of the following five categories:

  • English uppercase characters (A – Z)
  • English lowercase characters (a – z)
  • Base 10 digits (0 – 9)
  • Non-alphanumeric (For example: !, $, #, or %)
  • Unicode characters

How to add above rule in the validator rule?

My Code Here

// create the validation rules ------------------------
    $rules = array(
        'name'             => 'required',                        // just a normal required validation
        'email'            => 'required|email|unique:ducks',     // required and must be unique in the ducks table
        'password'         => 'required',
        'password_confirm' => 'required|same:password'           // required and has to match the password field
    );

    // do the validation ----------------------------------
    // validate against the inputs from our form
    $validator = Validator::make(Input::all(), $rules);

    // check if the validator failed -----------------------
    if ($validator->fails()) {

        // get the error messages from the validator
        $messages = $validator->messages();

        // redirect our user back to the form with the errors from the validator
        return Redirect::to('home')
            ->withErrors($validator);

    }

 Answers

4

I have had a similar scenario in Laravel and solved it in the following way.

The password contains characters from at least three of the following five categories:

  • English uppercase characters (A – Z)
  • English lowercase characters (a – z)
  • Base 10 digits (0 – 9)
  • Non-alphanumeric (For example: !, $, #, or %)
  • Unicode characters

First, we need to create a regular expression and validate it.

Your regular expression would look like this:

^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dx])(?=.*[!$#%]).*$

I have tested and validated it on this site. Yet, perform your own in your own manner and adjust accordingly. This is only an example of regex, you can manipluated the way you want.

So your final Laravel code should be like this:

'password' => 'required|
               min:6|
               regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dx])(?=.*[!$#%]).*$/|
               confirmed',

Update As @NikK in the comment mentions, in Laravel 5.5 and newer the the password value should encapsulated in array Square brackets like

'password' => ['required', 
               'min:6', 
               'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dx])(?=.*[!$#%]).*$/', 
               'confirmed']

I have not testing it on Laravel 5.5 so I am trusting @NikK hence I have moved to working with c#/.net these days and have no much time for Laravel.

Note:

  1. I have tested and validated it on both the regular expression site and a Laravel 5 test environment and it works.
  2. I have used min:6, this is optional but it is always a good practice to have a security policy that reflects different aspects, one of which is minimum password length.
  3. I suggest you to use password confirmed to ensure user typing correct password.
  4. Within the 6 characters our regex should contain at least 3 of a-z or A-Z and number and special character.
  5. Always test your code in a test environment before moving to production.
  6. Update: What I have done in this answer is just example of regex password

Some online references

  • http://regex101.com
  • http://regexr.com (another regex site taste)
  • https://jex.im/regulex (visualized regex)
  • http://www.pcre.org/pcre.txt (regex documentation)
  • http://www.regular-expressions.info/refquick.html
  • https://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspx
  • http://php.net/manual/en/function.preg-match.php
  • http://laravel.com/docs/5.1/validation#rule-regex
  • https://laravel.com/docs/5.6/validation#rule-regex

Regarding your custom validation message for the regex rule in Laravel, here are a few links to look at:

  • Laravel Validation custom message
  • Custom validation message for regex rule in Laravel?
  • Laravel custom validation messages
Saturday, October 8, 2022
1

If you are looking for validation of uniqueness of composite indexes (multiple columns), this is not possible unless you create a custom validation rule.

You can create a custom validation rule, see https://laravel.com/docs/validation#custom-validation-rules

// Example:
// 'col1' => 'unique_with:table,col2,col3,col4,etc'
// 'col2' => doesn't need to check uniqueness again, because we did it for col1

Validator::extend('unique_with', function ($attribute, $value, $parameters, $validator) {
    $request = request()->all();

  // $table is always the first parameter
  // You can extend it to use dots in order to specify: connection.database.table
  $table = array_shift($parameters);

  // Add current column to the $clauses array
  $clauses = [
    $attribute => $value,
  ];

  // Add the rest  
  foreach ($parameters as $column) {
    if (isset($request[$column])) {
        $clauses[$column] = $request[$column];
    }
  }

  // Query for existence.
  return ! DB::table($table)
            ->where($clauses)
            ->exists();
});

place that code in the boot() method of a service provider, you can use AppHttpProvidersAppServiceProvider.php I didn't test it, but it should help you to go forward and make the necessary adjustments.

Monday, November 14, 2022
3

Try this:

'field' => 'required|in:' . implode(',', $this->allslots),

Does that give you the expected result?

Monday, August 22, 2022
 
tiguchi
 
4

Docs don't make it clear, But removing required makes it work.

$this->validate($request, [
    'password' => 'sometimes|min:8',
]);
Monday, September 26, 2022
 
5

Really simple! Just add attributes in the language file! In my case, for lang/it/validation.php

I just set:

'attributes' => [
    'phone' => 'telefono',
],

while all remain the same for the sentences

'required' => 'Il campo :attribute ` obbligatorio.',
Monday, September 5, 2022
 
san
 
san
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :