Created
November 24, 2018 20:27
-
-
Save jobelenus/c1fecf86dc13d5ccb3c33cf880c433cd to your computer and use it in GitHub Desktop.
Working on elm
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 Browser | |
import Html exposing (Html, Attribute, div, input, text) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput) | |
import Regex exposing (..) | |
main = | |
Browser.sandbox | |
{ init = init | |
, update = update | |
, view = view | |
} | |
type Password = Pwd String | |
type PasswordError = NoPassword | NoSymbol | NoNumber | NoCapital | |
type alias User = | |
{ name : String | |
, typedPassword: String | |
, password: Password | |
} | |
digit : Regex.Regex | |
digit = | |
Maybe.withDefault Regex.never <| | |
Regex.fromString "[0-9]" | |
capital : Regex.Regex | |
capital = | |
Maybe.withDefault Regex.never <| | |
Regex.fromString "[A-Z]" | |
symbol : Regex.Regex | |
symbol = | |
Maybe.withDefault Regex.never <| | |
Regex.fromString "[!@#$%^&*(){}:;'<>,./?\"]" | |
init : User | |
init = | |
{ name = "" | |
, typedPassword = "" | |
, password = initPwd "" | |
} | |
initPwd : String -> Password | |
initPwd pass = | |
Pwd "" | |
checkPassword : String -> Result PasswordError Password | |
checkPassword pass = | |
-- test that we have a capital, number, and symbol | |
if Regex.contains digit pass == False then | |
Err (NoNumber) | |
else if Regex.contains capital pass == False then | |
Err (NoCapital) | |
else if Regex.contains symbol pass == False then | |
Err (NoSymbol) | |
else | |
Ok (Pwd pass) | |
testPassword: String -> String | |
testPassword pass = | |
case (checkPassword pass) of | |
Err NoPassword -> | |
"Please enter a password" | |
Err NoNumber -> | |
"Passwords must contain number" | |
Err NoCapital -> | |
"Passwords must contain a capital letter" | |
Err NoSymbol -> | |
"Passwords must contain a symbol" | |
Ok newPassword -> | |
"Valid password" | |
type Msg | |
= TestPassword String | |
update : Msg -> User -> User | |
update msg model = | |
case msg of | |
TestPassword newPassword -> | |
{ model | typedPassword = newPassword } | |
case checkPassword newPassword of | |
Ok pwd -> | |
{ model | password = pwd } | |
Err _ -> | |
{ model | password = initPwd ""} | |
view : User -> Html Msg | |
view model = | |
div [] | |
[ input [ placeholder "Name", value model.name ] [] | |
, input [ type_ "password", placeholder "Password", value "", onInput TestPassword ] [] | |
, div [] [ text (testPassword model.typedPassword) ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting a ParseError on line 93.
I realized that with a concrete Password type that errors on invalid input, I needed to also store the string that the user types in to even pass it around to the multiple places that will want to perform that test. I already have a label testing for it, I am also imaging a button, that is unclickable until it is valid that submits a form, etc.
I'm realizing that
initPass
is dumb and I should use a Maybe instead.