Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | Source Code | 0 0
  1. module Main exposing (Issue, Model, Msg(..), PasswdFeedback, init, inp, main, update, validate, view)
  2.  
  3. import Browser
  4. import Html exposing (Html, div, input, pre, text)
  5. import Html.Attributes exposing (disabled, placeholder, style, type_, value)
  6. import Html.Events exposing (onInput)
  7. import Set
  8.  
  9.  
  10. main =
  11. Browser.sandbox { init = init, update = update, view = view }
  12.  
  13.  
  14. type alias Model =
  15. { name : String
  16. , pass : String
  17. , pass2 : String
  18. , fdbk : PasswdFeedback
  19. }
  20.  
  21.  
  22. type alias PasswdFeedback =
  23. { locked : Bool
  24. , narration : List String
  25. }
  26.  
  27.  
  28. type alias Issue =
  29. { hide : Bool
  30. , explaination : String
  31. }
  32.  
  33.  
  34. init : Model
  35. init =
  36. Model "" "" "" (PasswdFeedback True [])
  37.  
  38.  
  39. type Msg
  40. = DelName String
  41. | DelPass String
  42. | DelPass2 String
  43.  
  44.  
  45. view model =
  46. div []
  47. [ inp "text" "Name" model.name DelName False
  48. , inp "password" "Password" model.pass DelPass False
  49. , inp "password" "Password2" model.pass2 DelPass2 model.fdbk.locked
  50. , pre [ style "color" "red" ] [ text (String.join "\n" model.fdbk.narration) ]
  51. ]
  52.  
  53.  
  54. update msg model =
  55. case msg of
  56. DelName x ->
  57. { model | name = x }
  58.  
  59. DelPass x ->
  60. { model | pass = x, fdbk = validate x }
  61.  
  62. DelPass2 x ->
  63. { model | pass2 = x }
  64.  
  65.  
  66. inp : String -> String -> String -> (String -> msg) -> Bool -> Html msg
  67. inp t ph val signal q =
  68. input [ type_ t, placeholder ph, value val, onInput signal, disabled q ] []
  69.  
  70.  
  71. validate : String -> PasswdFeedback
  72. validate passwd =
  73. let
  74. isCaps c =
  75. String.contains (String.fromChar c) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  76.  
  77. isLower c =
  78. String.contains (String.fromChar c) "abcdefghijklmnopqrstuvwxyz"
  79.  
  80. isPunct c =
  81. String.contains (String.fromChar c) "~!@#$%^&*()-_=+[]{}'\\;:,./<>?|"
  82.  
  83. isDigit c =
  84. Maybe.withDefault -1 (String.toInt (String.fromChar c)) >= 0
  85.  
  86. -- isDigit : Char -> Bool
  87. nonZero =
  88. String.length passwd > 0
  89.  
  90. lenOk =
  91. Issue (String.length passwd > 7) "Password too small."
  92.  
  93. hasDigit =
  94. Issue (String.any isDigit passwd) "No digits."
  95.  
  96. hasCapital =
  97. Issue (String.any isCaps passwd) "No capitals."
  98.  
  99. hasLower =
  100. Issue (String.any isLower passwd) "No lower case."
  101.  
  102. hasPunct =
  103. Issue (String.any isPunct passwd) "No punctuation."
  104.  
  105. worthShowing issue =
  106. if issue.hide == False then
  107. Just issue.explaination
  108.  
  109. else
  110. Nothing
  111.  
  112. goodPass =
  113. List.all .hide [ lenOk, hasDigit, hasCapital, hasLower, hasPunct ]
  114. in
  115. if nonZero && goodPass then
  116. PasswdFeedback False []
  117. -- else if ...the 32 cases
  118.  
  119. else
  120. PasswdFeedback True
  121. (List.filterMap worthShowing [ lenOk, hasDigit, hasCapital, hasLower, hasPunct ])
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement