Skip to content

Instantly share code, notes, and snippets.

@doppioslash
Last active March 11, 2017 03:07
Show Gist options
  • Save doppioslash/15c0c93be56d49d4f98f5795dea08b78 to your computer and use it in GitHub Desktop.
Save doppioslash/15c0c93be56d49d4f98f5795dea08b78 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 (length)
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 action model =
case action 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
renderReq : (String, String) -> Html msg
renderReq (colour, message) =
p [style [("color", colour)]] [text message]
checkForRequisites : String -> String -> List (String -> String -> (String, String)) -> List (String, String)
checkForRequisites pass matchpass reqs =
List.map (\req -> req pass matchpass) reqs
matchReq : String -> String -> (String, String)
matchReq pass matchpass =
if pass == matchpass then
("green", "Passwords match: OK")
else
("red", "Passwords do not match!")
lengthReq : String -> String -> (String, String)
lengthReq pass matchpass =
if length( pass ) >= 10 then
("green", "Password length: OK")
else
("red", "Password too short!")
reqs : List (String -> String -> (String, String))
reqs = [matchReq, lengthReq]
checked : List (String, String)
checked = checkForRequisites model.password model.passwordAgain reqs
in
div [ ] (List.map renderReq checked)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment