Created
November 11, 2016 22:38
-
-
Save jedschneider/d3685edcad7b69eb2a2f55be545a1313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (..) | |
import Html.App as Html | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput) | |
import String exposing (..) | |
import Char | |
main = | |
Html.beginnerProgram | |
{ model = model | |
, view = view | |
, update = update | |
} | |
-- MODEL | |
type alias Model = | |
{ name : String | |
, password : String | |
, passwordAgain : String | |
} | |
model : Model | |
model = | |
Model "" "" "" | |
-- UPDATE | |
type Msg | |
= Name String | |
| Password String | |
| PasswordAgain String | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
Name name -> | |
{ model | name = name } | |
Password password -> | |
{ model | password = password } | |
PasswordAgain password -> | |
{ model | passwordAgain = password } | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ input [ type' "text", placeholder "Name", onInput Name ] [] | |
, input [ type' "password", placeholder "Password", onInput Password ] [] | |
, input [ type' "password", placeholder "Re-enter Password", onInput PasswordAgain ] [] | |
, viewValidation model | |
] | |
viewValidation : Model -> Html msg | |
viewValidation model = | |
let | |
validations = | |
[ (passwordsMatch model.password model.passwordAgain) | |
, (passwordLongEnough model.password) | |
, (containsNumeric model.password) | |
] | |
(color, message) = | |
case modelValid validations of | |
True -> ("green", "OK") | |
False -> ("red", (join " " (validationMessages validations))) | |
in | |
div [ style [("color", color)] ] [ text message ] | |
-- HELPERS | |
validationMessages: List (Bool, String) -> List String | |
validationMessages list = | |
let | |
reducer = List.map snd >> List.filter (isEmpty >> not) | |
in | |
reducer list | |
modelValid: List (Bool, String) -> Bool | |
modelValid list = | |
List.all fst list | |
-- VALIDATIONS | |
passwordsMatch: String -> String -> (Bool, String) | |
passwordsMatch str again = | |
case str == again of | |
False -> (False, "Passwords do not match!") | |
True -> (True, "") | |
passwordLongEnough: String -> (Bool, String) | |
passwordLongEnough password = | |
case (String.length password) >= 8 of | |
True -> (True, "") | |
False -> (False, "Password is not long enough!") | |
containsNumeric: String -> (Bool, String) | |
containsNumeric password = | |
let | |
str = filter Char.isDigit password | |
in | |
case String.isEmpty str of | |
True -> (False, "Password must contain number!") | |
False -> (True, "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment