function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'user_pass_reset') {
$form['#validate'][] = '_form_custom_validation';
}
}
//CUSTOM FUNCTION TO CHECK PASSWORD COMPLEXITY
function _form_custom_validation(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
if ($form_state->hasValue('pass')) {
$pass = $form_state->getValue('pass');
$pattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$";
if (!preg_match('/[a-z]+/', $pass)) {
$form_state->setErrorByName('title', t('Your password must contain Uppercase, lowercase, numeric and special character.'));
}
if (!preg_match('/[A-Z]+/', $pass)) {
$form_state->setErrorByName('title', t('Your password must contain Uppercase, lowercase, numeric and special character.'));
}
if (!preg_match('/[0-9]/', $pass)) {
$form_state->setErrorByName('title', t('Your password must contain Uppercase, lowercase, numeric and special character.'));
}
if (!preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $pass)) {
$form_state->setErrorByName('title', t('Your password must contain Uppercase, lowercase, numeric and special character.'));
}
if (strlen($pass) < 8) {
$form_state->setErrorByName('title', t('Your password must contain atleast 8 characters.'));
}
}
}