'Sign Up validation Instagram (iOS)

I was given a signUp validation task. in doing so I tried my best. but I would like you to offer a better way or suggestion.

exception: Due to the correct email format, doSignUp () [Home page] is logged, even if the password format is incorrect

thanks for the attention

this is a view of my attempts 👇

    @State var fullname = "Vignesh"
    @State var email = "[email protected]"
    @State var password = ""
    @State var cpassword = ""
    @State var showError1 = false
    @State var showError2 = false
    @State var shouldHide = false

let emailFormat = NSPredicate(format: "SELF MATCHES %@", "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}")
    let passwordFormat = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[A-Z])(?=.*[a-z])(?=.*?[0-9])(?=.*[$@$#!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}")

TextField and SecureField

  VStack(alignment: .leading) {
                        TextField("email", text: $email)
                            .frame(height: 45)
                            .padding(.leading)
                            .background(.white.opacity(0.4))
                            .cornerRadius(10)
                            .textInputAutocapitalization(.never)
                        
                        if showError1 == true {
                            Text("Email is Not Valid")
                                .font(.footnote)
                                .fontWeight(.semibold)
                                .foregroundColor(.red)
                                .opacity(shouldHide ? 0 : 1)
                        }
                    }
                    
                    VStack(alignment: .leading) {
                        SecureField("password", text: $password)
                            .frame(height: 45)
                            .padding(.leading)
                            .background(.white.opacity(0.4))
                            .cornerRadius(10)
                        
                        if showError2 == true {
                            Text("Password is Not Valid")
                                .font(.footnote)
                                .fontWeight(.semibold)
                                .foregroundColor(.red)
                                .opacity(shouldHide ? 0 : 1)
                        }
                    }

Sign Up button

Button(action: {
                        if !emailFormat.evaluate(with: email) {
                            print("Check your email. And try again !!")
                            self.showError1 = true
                            self.shouldHide = false
                        } else {
                            self.showError1 = false
                            self.shouldHide = true
                            doSignUp()
                        }
                        
                        if !passwordFormat.evaluate(with: password) {
                            print("Check your password. And try again !!")
                            self.showError2 = true
                            self.shouldHide = false
                        } else {
                            self.showError2 = false
                            self.shouldHide = true
                            doSignUp()
                        }
                    }, label: {
                        Text("sign_up")
                            .frame(maxWidth: .infinity).frame(height: 50)
                            .foregroundColor(.white)
                            .background(RoundedRectangle(cornerRadius: 10)
                                            .stroke(lineWidth: 1.5)
                                            .foregroundColor(.white.opacity(0.4)))
                    })


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source