快速验证文本字段 3
Posted
技术标签:
【中文标题】快速验证文本字段 3【英文标题】:Validate text fields swift 3 【发布时间】:2016-12-07 16:56:25 【问题描述】:我正在尝试使我的所有文本字段都成为必需的,以确保用户不会将其留空。我试过这个,但由于某种原因它不起作用。我觉得我的逻辑是正确的,但这是swift的逻辑问题
这是我的代码:
let title = txtTitle.text!
let location = txtWhere.text!
let when = txtWhen.text!
if title != nil && location != nil && when != nil
var titleArray = [String]()
var whereArray = [String]()
var whenArray = [String]()
let titleObject = UserDefaults.standard.object(forKey: "saveTitle")
let whereObject = UserDefaults.standard.object(forKey: "saveLocation")
let whenObject = UserDefaults.standard.object(forKey: "saveWhen")
if let tempTitle = titleObject as? [String]
titleArray = tempTitle
titleArray.append(txtTitle.text!)
print(titleArray)
else
titleArray = [txtTitle.text!]
if let tempWhere = whereObject as? [String]
whereArray = tempWhere
whereArray.append(txtWhere.text!)
//print(titleArray)
else
whereArray = [txtWhere.text!]
if let tempWhen = whenObject as? [String]
whenArray = tempWhen
whenArray.append(txtWhen.text!)
//print(titleArray)
else
whenArray = [txtWhen.text!]
UserDefaults.standard.set(titleArray, forKey: "saveTitle")
UserDefaults.standard.set(whereArray, forKey: "saveLocation")
UserDefaults.standard.set(whenArray, forKey: "saveWhen")
txtTitle.text = ""
txtWhere.text = ""
txtWhen.text = ""
txtTime.text = ""
else
errMsg.isHidden = false
errMsg.text = "All fields are required"
【问题讨论】:
试试这个,例如txtTitle.text?.characters.count > 0
。请重构您的代码..注意强制解包选项!
@ThomasG.,如果您使用SwiftLint,它将不鼓励将count
与0 进行比较,并推荐isEmpty
。 ;)
【参考方案1】:
为了可靠地检查文本字段的text
属性是否不是nil
并且不是空的使用
if let title = txtTitle.text, !title.isEmpty,
let location = txtWhere.text, !location.isEmpty,
let when = txtWhen.text, !when.isEmpty ...
如果所有条件都通过,则三个变量被安全地解包。
【讨论】:
【参考方案2】:一个。您不应该强制展开 (txtTitle.text!
):而是检查可选值
b.您可能想要检查空字符串,而不仅仅是 nil
也就是说,您可以这样测试有效性:
let title = txtTitle.text
if title?.isEmpty == false
...// title was filled
或者更好(谢谢 Leo Dabus):
if let title = txtTitle.text, !title.isEmpty
...// title was filled
【讨论】:
if let title = txtTitle.text, !title.isEmpty
【参考方案3】:
文本字段验证 ======================>
func textFieldShouldReturn(_ textField: UITextField) -> Bool
switch (textField.tag)
case 1:
password.becomeFirstResponder()
case 2 :
confirm_pass.becomeFirstResponder()
case 3 :
email.becomeFirstResponder()
case 4 :
phone.becomeFirstResponder()
case 5 :
phone.resignFirstResponder()
default: break
return true
func textFieldShouldClear(_ textField: UITextField) -> Bool
return true
func textFieldDidEndEditing(_ textField: UITextField)
if (textField == username)
let name_reg = "[A-Za-z0-9]5,20"
let name_test = NSPredicate(format: "SELF MATCHES %@", name_reg)
if name_test.evaluate(with: username.text) == false
let alert = UIAlertController(title: "Information", message: "Enter the name in correct format", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(ok)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
if (textField == password)
let name_reg = "[A-Z0-9a-z._%@+-]6,10"
let name_test = NSPredicate(format: "SELF MATCHES %@", name_reg)
if name_test.evaluate(with: password.text) == false
let alert = UIAlertController(title: "Information", message: "Enter the password in correct format", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(ok)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
if (textField == email)
let name_reg = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]2,"
let name_test = NSPredicate(format: "SELF MATCHES %@", name_reg)
if name_test.evaluate(with: email.text) == false
let alert = UIAlertController(title: "Information", message: "Enter the E-mail in correct format", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(ok)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
if (textField == phone)
let name_reg = "[0-9]10"
let name_test = NSPredicate(format: "SELF MATCHES %@", name_reg)
if name_test.evaluate(with: phone.text) == false
let alert = UIAlertController(title: "Information", message: "Enter your number in correct format", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alert.addAction(ok)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
【讨论】:
欢迎来到 ***。虽然这个答案可能是正确的,但从长远来看,留下一些解释会改善这个问题。以上是关于快速验证文本字段 3的主要内容,如果未能解决你的问题,请参考以下文章