Post

Replies

Boosts

Views

Activity

Email validation using RegEx and TextField text color
Hello, I wrote a simple email validation test: struct ContentView: View {     @State private var email: String     @State var emailIsValid: Bool = true          public init(email: String = "")     {         self.email = email     }          var body: some View {         Text("Hello, world!")             .padding()                  TextField("Email", text: $email)             .onChange(of: email) { newValue in                 if(newValue.range(of:"^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", options: .regularExpression) != nil) {                     self.emailIsValid = true                     print("valid")                 } else {                     self.emailIsValid = false                     print("invalid")                 }             }             .foregroundColor(emailIsValid ? Color.green : Color.red)     } } And it works for the email like Test@gmail.com. Text is green. But if I type something like Testerka@gmail.com text stays red. But the print output says 'valid'.
3
0
3.4k
Aug ’22