Validate text fields swift 3

夙愿已清 提交于 2019-12-08 02:53:26

To check reliably if the text properties of the text fields are not nil and not empty use

if let title = txtTitle.text, !title.isEmpty,
   let location = txtWhere.text, !location.isEmpty,
   let when = txtWhen.text, !when.isEmpty { ...

If all conditions are passed the three variables are safely unwrapped.

a. You shouldn't force unwrap (txtTitle.text!): instead check the optional value

b. You may want to check for empty strings instead of just for nil

That said, you can test for validity like that:

let title = txtTitle.text
if title?.isEmpty == false {
    ...// title was filled
}

Or better (thank you Leo Dabus):

if let title = txtTitle.text, !title.isEmpty {
    ...// title was filled
}

TEXTFIELD VALIDATION ======================>

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)
        }
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!