Checking If Text Fields Are Empty Cause Error in Swift 2

Checking if text fields are empty cause error in Swift 2

Try this....

if txtEmail.text?.isEmpty == true || txtPassword.text?.isEmpty == true || txtRePassword.text?.isEmpty == true{
print("true")
}

how to check textfield is not empty and button enable

I have attached the code how to check that textFiled is empty or not

let txtField = UITextField()
txtField.text = "testing"
if txtField.text != "" {
btn_Pause.isEnabled = true
} else {
btn_Pause.isEnabled = false
}

-> Using isEmpty function

if txtField.text?.isEmpty == true {
btn_Pause.isEnabled = false
} else {
btn_Pause.isEnabled = true
}

-> Using character count

if txtField.text?.count ?? 0 > 0 {
btn_Pause.isEnabled = true
} else {
btn_Pause.isEnabled = false
}

Checking for filled text field & textview isn't working properly (UIKit)

Okay after a while, I managed to fix that, the reason it happened was because I thought my text is nil while it really was just an empty string.
So after adding extra validating to make sure to check for the empty case as well, I managed to get it to work perfectly fine.

Show message if all textfields are complete, correctly in swift 4?

You are not checking if the second text field is empty or not anywhere. ( I have no clue why you have named a text field that is supposed to store a name is named as filterTextField )

if isEmailAddressValid && !filterTextField.text?.isEmpty {
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
return
}

Unable to check for empty textfields in IOS swift app

Please make sure that all the outlets are connected properly.

Problem

The possible cause of the crash can be..

  • The result of firstNameTextField.text?.isEmpty may be null and you have used ! to unwrap so it get crashed

  • Or may possible that the IBOutlet is not connected to view controller in storyboard.

Solution

You can user if-let like below...

let fanme = firstNameTextField.text ?? ""
let lname = lastNameTextField.text ?? ""
let email = emailTextField.text ?? ""
let password = passwordTextField.text ?? ""

if fname.isEmpty || lname.isEmpty || email.isEmpty || password.isEmpty {
userMessage(userMessage: "All fields are required")
return
}

swift multiple conditions if statement with && operation

you can simply use isEmpty property.

if !self.textFieldOne.text!.isEmpty && !self.textFieldTwo.text!.isEmpty && !self.textFieldThree.text!.isEmpty && !self.textFieldFour.text!.isEmpty {
...

}

or you can also safely unwrap the text value and the check that it is empty of not

if let text1 = self.textFieldOne.text, text2 = self.textFieldTwo.text, text3 = self.textFieldthree.text,text4 = self.textFieldFour.text where !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
...
}

Or you can compare with Empty "" String

if self.textFieldOne.text != "" && self.textFieldTwo.text != "" && self.textFieldThree.text != "" && self.textFieldFour.text != ""   {
...
}

and we can also do this with Guard

guard let text = self.myTextField.text  where !text.isEmpty else {
return
}

Objective c checking whether text field is empty

Simply checks for nil and if length of text length is greater than 0 - not empty

if (textField.text && textField.text.length > 0)
{
/* not empty - do something */
}
else
{
/* what ever */
}

How to know if a textfield is empty in Swift?

age is of type Int? (an optional Int), so is either an integer or nil. You cannot treat a nil comparison like a Boolean comparison in Swift if statements. You must explicitly check against nil:

if age != nil {
let humanAge = age! * 7
//...
} else {
//...
}

But you should probably use optional binding instead, unless you have a reason not to:

if let age = edadGatoTextField.text.toInt() {
let humanAge = age * 7
//...
} else {
//...
}


Related Topics



Leave a reply



Submit