Skip to content

Instantly share code, notes, and snippets.

@jedschneider
Created November 11, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedschneider/d3685edcad7b69eb2a2f55be545a1313 to your computer and use it in GitHub Desktop.
Save jedschneider/d3685edcad7b69eb2a2f55be545a1313 to your computer and use it in GitHub Desktop.
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