From 9fd236c5035484e21a29051cb4153f489ea0166d Mon Sep 17 00:00:00 2001 From: eduardoquintero117 Date: Fri, 12 Jan 2024 20:23:55 -0600 Subject: [PATCH] Modify Signup Input And Validation Rules --- SwiftUIBasics/Views/SignUpView.swift | 81 ++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/SwiftUIBasics/Views/SignUpView.swift b/SwiftUIBasics/Views/SignUpView.swift index 6cef5d9..335a2b7 100644 --- a/SwiftUIBasics/Views/SignUpView.swift +++ b/SwiftUIBasics/Views/SignUpView.swift @@ -10,26 +10,38 @@ import Combine class SignUpViewModel: ObservableObject { // inputs - @Published var username: String = "" + @Published var email: String = "" @Published var password: String = "" @Published var passwordConfirm: String = "" // outputs - @Published var isValidUsernameLength: Bool = false + @Published var isValidUserEmail: Bool = false @Published var isValidPasswordLength: Bool = false @Published var isValidPasswordUpperCase: Bool = false @Published var isValidPasswordMatch: Bool = false @Published var isValid: Bool = false + + //Rules + + @Published var isValidPasswordLowerCase: Bool = false + @Published var isValidPasswordSymbol: Bool = false + @Published var isValidPasswordNumber: Bool = false + @Published var isValidPasswordCharacter: Bool = false private var cancelableSet: Set = [] init() { - $username + $email .receive(on: RunLoop.main) - .map { username in - return username.count >= 4 + .map { email in + let pattern = #"^\S+@\S+\.\S+$"# + if let _ = email.range(of: pattern, options: .regularExpression) { + return true + } else { + return false + } } - .assign(to: \.isValidUsernameLength, on: self) + .assign(to: \.isValidUserEmail, on: self) .store(in: &cancelableSet) $password @@ -52,6 +64,47 @@ class SignUpViewModel: ObservableObject { } .assign(to: \.isValidPasswordUpperCase, on: self) .store(in: &cancelableSet) + + $password + .receive(on: RunLoop.main) + .map { password in + let pattern = "[a-z]" + if let _ = password.range(of: pattern, options: .regularExpression) { + return true + } else { + return false + } + } + .assign(to: \.isValidPasswordLowerCase, on: self) + .store(in: &cancelableSet) + + $password + .receive(on: RunLoop.main) + .map { password in + let pattern = "[0-9]" + if let _ = password.range(of: pattern, options: .regularExpression) { + return true + } else { + return false + } + } + .assign(to: \.isValidPasswordNumber, on: self) + .store(in: &cancelableSet) + + $password + .receive(on: RunLoop.main) + .map { password in + let pattern = "[! # $ % & ' ( ) * + , -. / : ; < = > ? @ [ ] ^ _ ` { | }]" + if let _ = password.range(of: pattern, options: .regularExpression) { + return true + } else { + return false + } + } + .assign(to: \.isValidPasswordSymbol, on: self) + .store(in: &cancelableSet) + + Publishers.CombineLatest($password, $passwordConfirm) .receive(on: RunLoop.main) @@ -61,7 +114,14 @@ class SignUpViewModel: ObservableObject { .assign(to: \.isValidPasswordMatch, on: self) .store(in: &cancelableSet) - Publishers.CombineLatest4($isValidUsernameLength, $isValidPasswordLength, $isValidPasswordUpperCase, $isValidPasswordMatch) + Publishers.CombineLatest4($isValidUserEmail, $isValidPasswordLength, $isValidPasswordUpperCase, $isValidPasswordMatch) + .map { (a, b, c, d) in + return a && b && c && d + } + .assign(to: \.isValid, on: self) + .store(in: &cancelableSet) + + Publishers.CombineLatest4($isValidUserEmail, $isValidPasswordLowerCase, $isValidPasswordSymbol, $isValidPasswordNumber) .map { (a, b, c, d) in return a && b && c && d } @@ -80,13 +140,16 @@ struct SignUpView: View { .bold() .foregroundStyle(.maryBlue) .padding(.bottom, 30) - FormTextField(name: "Username", value: $vm.username) - RequirementText(text: "A minimum of 4 characters", isValid: vm.isValidUsernameLength) + FormTextField(name: "E–mail", value: $vm.email) + RequirementText(text: "Email is valid", isValid: vm.isValidUserEmail) .padding() FormTextField(name: "Password", value: $vm.password, isSecure: true) VStack { RequirementText(text: "A minimum of 8 characters", isValid: vm.isValidPasswordLength) RequirementText(text: "One uppercase letter", isValid: vm.isValidPasswordUpperCase) + RequirementText(text: "One lowercase letter", isValid: vm.isValidPasswordLowerCase) + RequirementText(text: "One symbol", isValid: vm.isValidPasswordSymbol) + RequirementText(text: "One number", isValid: vm.isValidPasswordNumber) } .padding() FormTextField(name: "Confirm Password", value: $vm.passwordConfirm, isSecure: true)