passwordVerify.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function Password(
  2. parent,
  3. open,
  4. withButton,
  5. id
  6. ) {
  7. const inputPassword = document.createElement('input');
  8. inputPassword.type = 'password';
  9. inputPassword.id = id;
  10. parent.appendChild(inputPassword);
  11. const buttonPassword = document.createElement('button');
  12. buttonPassword.type = 'button';
  13. buttonPassword.textContent = 'показать';
  14. if(withButton) {
  15. parent.appendChild(buttonPassword);
  16. }
  17. const buttonLogin = document.createElement('button');
  18. buttonLogin.type = 'button';
  19. buttonLogin.classList.add('login-button');
  20. buttonLogin.textContent = 'Логин';
  21. buttonLogin.disabled = true;
  22. const divider = document.createElement('br');
  23. buttonPassword.addEventListener('click', () => {
  24. this.setOpen(open !== true);
  25. });
  26. this.setValue = newValue => inputPassword.value = newValue;
  27. this.getValue = () => inputPassword.value;
  28. this.setOpen = openUpdate => {
  29. open = openUpdate;
  30. this.onOpenChange(openUpdate);
  31. buttonPassword.textContent = (openUpdate) ? 'показать' : 'скрыть';
  32. inputPassword.type = (openUpdate) ? 'password' : 'text';
  33. }
  34. this.getOpen = () => open;
  35. inputPassword.addEventListener('input', event => {
  36. if (typeof this.onChange === 'function'){
  37. this.onChange(event.target.value);
  38. }
  39. });
  40. this.onOpenChange = isOpen => isOpen;
  41. this.addButton = () => parent.appendChild(buttonLogin);
  42. this.addDivider = () => parent.appendChild(divider);
  43. }
  44. const password1 = new Password(document.body, true, true, 'first_password');
  45. password1.addDivider();
  46. const password2 = new Password(document.body, true, false, 'second_password');
  47. password2.addDivider();
  48. password1.addButton();
  49. const isEqual = () => {
  50. document.querySelector('.login-button').disabled = (password1.getValue() !== password2.getValue());
  51. }
  52. password1.onOpenChange = (open) => {
  53. second_password.style.display = !open ? 'none' : 'inline-block';
  54. }
  55. password1.onChange = () => {
  56. isEqual();
  57. }
  58. password2.onChange = () => {
  59. isEqual();
  60. }