How I can implement a strong policy password

Hello,

Can you tell me how I can implement a strong policy password like :

  • 8 characteres minimum

  • At least one special charactere

  • At least one number

Many thanks !

Hi,

Please try using this code snippet as a sample.

add_filter(
	'hivepress/v1/forms/user_login/errors',
	function( $errors, $values ) {
		if ( isset( $values['password'] ) ) {
			$password = $values['password'];

			if ( ! preg_match( '/\d/', $password ) ) {
				$errors[] = 'Password should contain a digit.';
			}

			if ( ! preg_match( '/[^a-zA-Z\d]/', $password ) ) {
				$errors[] = 'Password should contain a special character.';
			}
		}

		return $errors;
	},
	10,
	2
);

Hi,

Thank you for the php snipped code, but it doesn’t work for me. Do you have another snipped code to provide me please ?

Many thanks!

I think that line needs to be replaced with

Blockquote

hivepress/v1/forms/user_register/errors

Blockquote

Even after the change it does not work, interesting

Please try this code snippet instead:

add_filter(
	'hivepress/v1/forms/user_register/errors',
	function( $errors, $form ) {
		$values = $form->get_values();

		if ( isset( $values['password'] ) ) {
			$password = $values['password'];

			if ( ! preg_match( '/\d/', $password ) ) {
				$errors[] = 'Password should contain a digit.';
			}

			if ( ! preg_match( '/[^a-zA-Z\d]/', $password ) ) {
				$errors[] = 'Password should contain a special character.';
			}
		}

		return $errors;
	},
	10,
	2
);

Hello @ihor,

Thank you for sharing this snippet. It worked for me and answered the question I asked here earlier: Enforce strong password on user creation

To keep users from generating weak passwords during further use of their account, I apply the filter to the user_update and the user_password_reset form as well.

However, there is an inconsistency now in the UI since the default JavaScript function and prompt of the forms consider the password length only. How can it be changed to include other requirements like “at least one special character” and “at least one number”?

Thanks!

Hi,

Please note that the snippet provided by Igor checks whether there is at least one digit and one special character in the password. Also, this is PHP validation (i.e. when you click on the button to confirm registration or reset your password, you will see an error if you have not followed the appropriate rules). That is, this is not a JS validation, as in the default WordPress login form. And in order to add such a JS validation, it will require a custom implementation, but we plan to use this JS script that WordPress uses.

Hi @andrii,

Thank you for your reply.

If I just want to change the minimum password length (e.g. to 12) and the number in the prompt accordingly, this solution worked for me:

/* Change minimum password length */
/* Source: https://docs.hivepress.io/developer-docs/framework/fields */
add_filter(
	'hivepress/v1/fields/password',
	function( $field ) {
		$field['min_length'] = '12';

		return $field;
	}
);

Cheers!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.