Dz10js.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. function Password(parent, open = false) {
  12. const input = document.createElement("input");
  13. const checkbox = document.createElement("input");
  14. input.type = "password";
  15. checkbox.type = "checkbox";
  16. parent.append(input);
  17. parent.append(checkbox);
  18. this.setValue = (value) => (input.value = value);
  19. this.getValue = () => input.value;
  20. this.setOpen = () => {
  21. if (input.type === "text") {
  22. input.type = "password";
  23. } else {
  24. input.type = "text";
  25. }
  26. };
  27. this.getOpen = () => (input.type === "text" ? true : false);
  28. input.oninput = (event) => {
  29. if (typeof this.onChange === "function") {
  30. this.onChange(input.value);
  31. }
  32. if (
  33. typeof this.onOpenChange === "function" &&
  34. value !== input.value &&
  35. this.getOpen() === true
  36. ) {
  37. this.onOpenChange(value);
  38. }
  39. };
  40. checkbox.oninput = (event) => {
  41. if (typeof this.onChange === "function") {
  42. this.onChange(checkbox.checked);
  43. }
  44. };
  45. checkbox.onclick = (event) => this.setOpen();
  46. this.getInput = () => input;
  47. }
  48. let p = new Password(document.body, true);
  49. p.onChange = (data) => console.log(data);
  50. p.onOpenChange = open => console.log(open)
  51. p.setValue('qwerty')
  52. console.log(p.getValue())
  53. p.setOpen(false)
  54. console.log(p.getOpen())
  55. //LoginForm
  56. function LoginForm(parent) {
  57. const form = document.createElement("form");
  58. const login = document.createElement("input");
  59. const password = document.createElement("input");
  60. const checkbox = document.createElement("input");
  61. const btnSubmit = document.createElement("button");
  62. password.type = "password";
  63. checkbox.type = "checkbox";
  64. btnSubmit.innerText = "Truth";
  65. btnSubmit.disabled = true;
  66. parent.append(form);
  67. form.append(login);
  68. form.append(password);
  69. form.append(checkbox);
  70. form.append(btnSubmit);
  71. this.changeVisiblePassword = () => {
  72. if (password.type === "text") {
  73. password.type = "password";
  74. } else {
  75. password.type = "text";
  76. }
  77. };
  78. checkbox.onclick = (event) => this.changeVisiblePassword();
  79. this.checkBtnSend = () => {
  80. if (!login.value || !password.value) {
  81. btnSubmit.disabled = true;
  82. } else {
  83. btnSubmit.disabled = false;
  84. }
  85. };
  86. login.oninput = (event) => this.checkBtnSend();
  87. password.oninput = (event) => this.checkBtnSend();
  88. }
  89. const logForm = new LoginForm(document.body);
  90. //LoginForm Constructor
  91. function LoginFormAdvance(parent) {
  92. const form = document.createElement("form");
  93. const login = document.createElement("input");
  94. const password = document.createElement("input");
  95. const checkbox = document.createElement("input");
  96. const btnSubmit = document.createElement("button");
  97. password.type = "password";
  98. checkbox.type = "checkbox";
  99. btnSubmit.innerText = "Truth";
  100. btnSubmit.disabled = true;
  101. parent.append(form);
  102. form.append(login);
  103. form.append(password);
  104. form.append(checkbox);
  105. form.append(btnSubmit);
  106. this.setValueLogin = (value) => (login.value = value);
  107. this.setValuePassword = (value) => (password.value = value);
  108. this.getValueLogin = () => login.value;
  109. this.getPasswordLogin = () => password.value;
  110. this.setOpenPassword = () => {
  111. if (password.type === "text") {
  112. password.type = "password";
  113. } else {
  114. password.type = "text";
  115. }
  116. };
  117. this.getOpenPassword = () => (password.type === "text" ? true : false);
  118. this.setVisibleBtn = () => (btnSubmit.disabled = !btnSubmit.disabled);
  119. this.getVisibleBtn = () => (btnSubmit.disabled ? false : true);
  120. this.checkBtnSend = () => {
  121. if (!login.value || !password.value) {
  122. btnSubmit.disabled = true;
  123. } else {
  124. btnSubmit.disabled = false;
  125. }
  126. };
  127. login.oninput = (event) => {
  128. if (typeof this.onChange === "function" && value !== login.value) {
  129. this.onChange(value);
  130. }
  131. this.checkBtnSend();
  132. };
  133. password.oninput = (event) => {
  134. if (typeof this.onChange === "function" && value !== password.value) {
  135. this.onChange(value);
  136. }
  137. this.checkBtnSend();
  138. };
  139. checkbox.onclick = (event) => this.setOpenPassword();
  140. }
  141. const logFormAdv = new LoginFormAdvance(document.body);
  142. // Password Verify
  143. function PasswordVerify(parent) {
  144. const p1 = new Password(parent);
  145. const p2 = new Password(parent);
  146. const btn = document.createElement("button");
  147. btn.disabled = true;
  148. btn.innerHTML = "Truth";
  149. parent.append(btn);
  150. this.checkMatches = () => {
  151. if (p1.getValue() === p2.getValue()) {
  152. btn.disabled = false;
  153. } else {
  154. btn.disabled = true;
  155. }
  156. };
  157. p1.onChange = (value) => {
  158. if (value === false) {
  159. p2.getInput().style.visibility = 'visible';
  160. } else if (value === true) {
  161. p2.getInput().style.visibility = 'hidden';
  162. }
  163. this.checkMatches();
  164. };
  165. p2.onChange = (value) => this.checkMatches();
  166. }
  167. const passVeri = new PasswordVerify(document.body);
  168. </script>
  169. </body>
  170. </html>