这是我的代码我希望用户名不要有空格和小写 例子(wizkhalifa)
func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textFieldToChange == username {
let characterSetNotAllowed = CharacterSet.whitespaces
if let _ = string.rangeOfCharacter(from:NSCharacterSet.uppercaseLetters) {
return false
}
if let _ = string.rangeOfCharacter(from: characterSetNotAllowed, options: .caseInsensitive) {
return false
} else {
return true
}
}
if UserNameField.text != nil, emailField.text != nil, passwordField.text != nil, rePassword.text != nil && passwordField.text == rePassword.text {
Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
if error != nil {
AlertController.showAlert(self, titel: "Error", message: " Fill all fields \nUser exists ")
return
}
答案 0 :(得分:0)
您需要检查2件事情,并且可以在尝试创建用户时和/或用户键入时调用所调用的方法。
首先检查,空格:
if let text = UserNameField.text {
if text.components(separatedBy: " ").count > 1 && text.lowercased() != textField.text {// add your other tests on this line
// the text is in lowercase without space character and your tests pass
Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
// check if there is an error
})
} else {
AlertController.showAlert(self, titel: "Error", message: " Fill all fields \nUser exists ")
}
以下是OP的版本及其代码:
if UserNameField.text != nil, emailField.text != nil, passwordField.text != nil, rePassword.text != nil && passwordField.text == rePassword.text && UserNameField.text.components(separatedBy: " ").count <= 1 && UserNameField.text.lowercased() == UserNameField.text {
Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
if error != nil {
AlertController.showAlert(self, title: "Error", message: " Fill all fields \nUser exists ")
return
} else {
// all good
}
})
} else {
AlertController.showAlert(self, title: "Error", message: " Fill all fields \nUser exists ")
}