Password field check with JS

Asked

Viewed 40 times

0

I made a filter to check if the password field contained at least 6 characters, but now I need to return an error message if you do not have it.

I tried using map, because the application already contained a generic error, but I need one for this specific type of error

My password field is this:

.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
            input#newPassword.mdl-textfield__input(type="password" data-validation="password-register" name="newPassword")
            label.mdl-textfield__label(for="newPassword")= __('reset-password.form.new-password')
            span.mdl-textfield__error
  event.preventDefault();
  const invalidFields = getInvalidFields(event.target, {
    validations: {
      'password-register': {
        passwordPolicy: element => element.value.length >= 6,
        message: 'A senha deve ter ao menos 6 caracteres.',
      },
    },
  });

  if (invalidFields.length) {
    showErrorMessage(invalidFields);
    return;
  }

  Array.from(form.elements)
    .filter(element => element instanceof HTMLInputElement)
    .filter(input => (
      !input.value
      || (validations[input.dataset.validation]
        && !validations[input.dataset.validation].passwordPolicy(input))
    ))
);

export const showErrorMessage = invalidFields => (
  invalidFields.forEach((input) => {
    input.parentElement.lastElementChild.innerHTML = 'Campo obrigatório';
    input.passwordPolicy.innerHTML = 'Errrooooooooow'; //não funciona
    input.parentElement.classList.add('is-invalid');
  })
);
  • You can show this function getInvalidFields?

  • Hello Sergio! This function is being created in the 2 code block, right in the first lines

  • Flávia, I don’t see where this function is, here in the question... you can add?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.