hw14_07_password_verify.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <header>Password Verify</header>
  2. <body>
  3. <script>
  4. function Password(parent, open) {
  5. this.input = document.createElement("input");
  6. parent.append(this.input);
  7. this.setValue = function (value) {
  8. this.input.value = value;
  9. }
  10. this.getValue = function () {
  11. return this.input.value;
  12. }
  13. this.setOpen = function (open) {
  14. this.open = open;
  15. this.check.style.display = this.open === undefined ? "none" : "block";
  16. this.input.type = this.open === true ? "text" : "password";
  17. }
  18. this.getOpen = function () {
  19. return this.input.type == "text";
  20. }
  21. this.input.onchange = function () {
  22. if (this.onChange)
  23. this.onChange(this.getValue());
  24. }.bind(this);
  25. this.show = function (isShow) {
  26. let displayStyle = isShow ? "block" : "none";
  27. this.input.style.display = displayStyle;
  28. if (this.open !== undefined)
  29. this.check.style.display = displayStyle;
  30. }.bind(this);
  31. this.setErrorState = function (isError) {
  32. let borderColor = isError ? "#FF0000" : "#00FF00";
  33. this.input.style.borderColor = borderColor;
  34. }.bind(this)
  35. this.check = document.createElement("input");
  36. parent.append(this.check);
  37. this.check.onchange = function () {
  38. this.setOpen(this.check.checked);
  39. if (this.onOpenChange)
  40. this.onOpenChange(this.getOpen());
  41. }.bind(this);
  42. this.check.checked = open;
  43. this.check.value = "open";
  44. this.check.type = "checkbox";
  45. this.setOpen(open);
  46. }
  47. let passw1 = new Password(document.body, true);
  48. let passw2 = new Password(document.body);
  49. passw2.show(!passw1.getOpen());
  50. passw1.onOpenChange = function (isOpen) {
  51. passw2.show(!isOpen);
  52. if (passw1.getOpen())
  53. passw1.setErrorState(false);
  54. else
  55. passw1.onChange(passw1.getValue());
  56. }
  57. const checkPasswordsEqual = (val) => {
  58. if (!passw1.getOpen()) {
  59. var areEqual = passw1.getValue() == passw2.getValue();
  60. passw1.setErrorState(!areEqual);
  61. passw2.setErrorState(!areEqual);
  62. }
  63. }
  64. passw1.onChange = checkPasswordsEqual;
  65. passw2.onChange = checkPasswordsEqual;
  66. </script>
  67. </body>