I am creating my signup page by following the Firebase Authentication Tutorial 2020 and on my signup page, whenever I click create account without anything in any of the fields, my app crashes. Error code is: “Thread 1: Exception: “Can’t do regex matching, reason: (Can’t open pattern U_REGEX_RULE_SYNTAX (string , pattern ^(?=.[a-z])(?=.[A-Z])(?=.*\d)a-zA-z\d{8,32}$, case 0, canon 0))”” on the AppDelegate.swift file.
This is my code for the password validity check:
static func isPasswordValid(_ password : String) → Bool {
let passwordTest = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-z\\d](?=.*[0-9]){8,32}$")
return passwordTest.evaluate(with: password)
}
}
This is my code for the signup page:
import UIKit
import FirebaseAuth
import Firebase
import FirebaseFirestore
class SignUpP1ViewController: UIViewController {
@IBOutlet weak var FirstNameTextField: UITextField!
@IBOutlet weak var LastNameTextField: UITextField!
@IBOutlet weak var EmailTextField: UITextField!
@IBOutlet weak var PasswordTextField: UITextField!
@IBOutlet weak var ConfirmPasswordTextField: UITextField!
@IBOutlet weak var ContinueButton: UIButton!
@IBOutlet weak var AlreadyhaveaccountButton: UIButton!
@IBOutlet weak var ErrorLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let toolBar = UIToolbar ()
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.doneClicked))
toolBar.setItems([flexibleSpace, doneButton], animated: true)
FirstNameTextField.inputAccessoryView = toolBar
LastNameTextField.inputAccessoryView = toolBar
EmailTextField.inputAccessoryView = toolBar
PasswordTextField.inputAccessoryView = toolBar
ConfirmPasswordTextField.inputAccessoryView = toolBar
// Do any additional setup after loading the view.
setUpElements()
}
@objc func doneClicked() {
view.endEditing(true)
}
func setUpElements() {
ErrorLabel.alpha = 0
//Utilities.styleFilledButton(ContinueButton)
//Utilities.styleFilledButton(AlreadyhaveaccountButton)
}
func validateFields() -> String? {
if let FirstNameEmpty = FirstNameTextField.text, !FirstNameEmpty.isEmpty
{
return "Please fill in all fields"
}
/*
!(FirstNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)! ||
!(LastNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)! ||
!(EmailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)! ||
!(PasswordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)! ||
!(ConfirmPasswordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)!
*/
let cleanedPassword = PasswordTextField.text!
//.trimmingCharacters(in: .whitespacesAndNewlines)
if Utilities.isPasswordValid(cleanedPassword) == false {
return "Password has to have at minimum 1 capital letter, 1 lowercase letter, 1 number, and be between 8-32 characters"
}
return nil
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
@IBAction func ContinueButtonTapped(_ sender: Any) {
let error = validateFields()
if error != nil{
showError(error!)
}
else {
let firstname = FirstNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let lastname = LastNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let email = EmailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = PasswordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
if err != nil {
self.showError("Error creating user")
}
else {
let db = Firestore.firestore()
db.collection("users").addDocument(data: ["firstname":firstname, "lastname":lastname, "uid": result!.user.uid ]) { (error) in
if error != nil {
self.showError("Error saving user data, please contact support")
}
}
self.transitionToHome()
}
}
}
}
@IBAction func AlreadyhaveaccountTapped(_ sender: Any) {
}
func showError(_ message: String) {
ErrorLabel.text = message
ErrorLabel.alpha = 1
}
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
}