passwordVerify.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 divider = document.createElement('br');
  18. buttonPassword.addEventListener('click', () => {
  19. this.setOpen(open !== true);
  20. });
  21. this.setValue = newValue => inputPassword.value = newValue;
  22. this.getValue = () => inputPassword.value;
  23. this.setOpen = openUpdate => {
  24. open = openUpdate;
  25. if(typeof this.onOpenChange === 'function') {
  26. this.onOpenChange(openUpdate);
  27. }
  28. buttonPassword.textContent = (openUpdate) ? 'показать' : 'скрыть';
  29. inputPassword.type = (openUpdate) ? 'password' : 'text';
  30. }
  31. this.getOpen = () => open;
  32. inputPassword.addEventListener('input', event => {
  33. if (typeof this.onChange === 'function'){
  34. this.onChange(event.target.value);
  35. }
  36. });
  37. this.addDivider = () => parent.appendChild(divider);
  38. }
  39. const password1 = new Password(document.body, true, true, 'first_password');
  40. password1.addDivider();
  41. const password2 = new Password(document.body, true, false, 'second_password');
  42. password2.addDivider();
  43. const buttonLogin = document.createElement('button');
  44. buttonLogin.type = 'button';
  45. buttonLogin.textContent = 'Логин';
  46. buttonLogin.disabled = true;
  47. document.body.appendChild(buttonLogin);
  48. const isEqual = () => {
  49. buttonLogin.disabled = (password1.getValue() !== password2.getValue());
  50. }
  51. password1.onOpenChange = (open) => {
  52. second_password.style.display = !open ? 'none' : 'inline-block';
  53. }
  54. password1.onChange = () => {
  55. isEqual();
  56. }
  57. password2.onChange = () => {
  58. isEqual();
  59. }