正如你从这个问题中可以看出的那样,我是iOS/编程的新手:我正在尝试实现密码验证,其中用户输入必须至少为8个字符,包含1个大写字母、1个小写字母、一个数字和一个特殊字符。我决定使用正则表达式和UIViewController上的扩展来实现这一点我在某个地方读到,我必须将其包含在我的"SignUpViewController"范围之外(不确定为什么(这就是我所做的数据库确实有开放的读/写规则,因为我仍在测试,所以它没有每次检查验证并注册用户的原因是什么?请帮助
以下是代码:
import UIKit
import Firebase
class SignUpViewController: UIViewController {
//Outlets
@IBOutlet weak var firstNameText: UITextField!
@IBOutlet weak var lastNameText: UITextField!
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passwordText: UITextField!
@IBOutlet weak var signUpButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func signUpButtonTapped(_ sender: Any) {
guard let firstName = firstNameText.text,
let lastName = lastNameText.text,
let email = emailText.text else { return }
guard emailText.containsValidEmail() else {
print("Invalid email. Please try again.")
return
}
let password = passwordText.text else { return }
guard passwordText.containsValidPassword() else {
print("Invalid password. Please try again.")
return
}
Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
if let error = error {
debugPrint("Error creating user: (error.localizedDescription)")
}
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = firstName
changeRequest?.commitChanges(completion: { (error) in
if let error = error {
debugPrint(error.localizedDescription)
}
})
guard let userId = authResult?.user else { return }
let userData: [String: Any] = [
"firstName" : "",
"lastName" : "",
"User ID" : userId,
"dateCreated" : FieldValue.serverTimestamp(),
]
let db = Firestore.firestore()
db.collection("users").document("one").setData(userData) { err in
if let err = err {
print("Error writing document: (err)")
} else {
print("Document successfully written!")
}
}
}
}
}
public extension UITextField {
func containsValidPassword() -> Bool {
let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self.text)
}
}
public extension UITextField {
func containsValidEmail() -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self.text)
}
}
更新12/14:我通过添加密码和电子邮件验证的扩展名来编辑原始帖子。我使用UITextField上的扩展名添加了密码验证。然而,当我尝试对电子邮件执行同样的操作时,我会遇到编译错误。为什么会这样
首先,您不会在代码中的任何位置调用isValidPassword
函数。其次,如果您实际调用它,它将导致运行时异常,因为您将尝试将UIViewController
实例传递给NSPredicate
以在regex上匹配。
您应该在UITextField
上定义isValidPassword
,并验证其text
属性。(在这种情况下,我也会将函数重命名为containsValidPassword
,因为UITextField
不是密码,它只包含密码的字符串。(
extension UITextField {
func containsValidPassword() -> Bool {
let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self.text)
}
}
然后你还需要调用验证:
@IBAction func signUpButtonTapped(_ sender: Any) {
guard let firstName = firstNameText.text,
let lastName = lastNameText.text,
let email = emailText.text,
let password = passwordText.text else { return }
guard passwordText.containsValidPassword() else {
// Let the user know that their pwd is incorrect
return
}
...
使用self
评估谓词,其中self
的类型为UIViewController
(这就是您要扩展的类型(,您可能打算扩展String
。
extension String {
func isValidPassword() -> Bool {
let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
}
}
用法:
let isValidPassword = "something".isValidPassword()
事实上,我不确定扩展是否适合这种条件,我建议您在SignUpViewController
-中创建一个简单的函数
func isValidPassword(password: String) -> Bool {
let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: password)
}
或者如果你想更具体一点-
func isValidPassword() -> Bool{
guard let password = passwordText.text else { return false }
let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: password)
}
import UIKit
import Firebase
class SignUpViewController: UIViewController {
//Outlets
@IBOutlet weak var firstNameText: UITextField!
@IBOutlet weak var lastNameText: UITextField!
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passwordText: UITextField!
@IBOutlet weak var signUpButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func signUpButtonTapped(_ sender: Any) {
guard let firstName = firstNameText.text,
let lastName = lastNameText.text,
let email = emailText.text else { return }
guard emailText.containsValidEmail() else {
print("Invalid email/password. Please try again.")
return
}
guard let password = passwordText.text else { return }
guard passwordText.containsValidPassword() else {
print("Invalid email/password. Please try again.")
return
}
Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
if let error = error {
debugPrint("Error creating user: (error.localizedDescription)")
}
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = firstName
changeRequest?.commitChanges(completion: { (error) in
if let error = error {
debugPrint(error.localizedDescription)
}
})
guard let userId = authResult?.user else { return }
let userData: [String: Any] = [
"firstName" : "",
"lastName" : "",
"User ID" : userId,
"dateCreated" : FieldValue.serverTimestamp(),
]
let db = Firestore.firestore()
db.collection("users").document("one").setData(userData) { err in
if let err = err {
print("Error writing document: (err)")
} else {
print("Document successfully written!")
}
}
}
}
}
public extension UITextField {
func containsValidPassword() -> Bool {
let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self.text)
}
}
public extension UITextField {
func containsValidEmail() -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self.text)
}
}
此格式按预期运行。谢谢