Multiple Text Fields Invalid Entry Problem

REFERENCE SCREENSHOT:

h ttps://drive.google.com/file/d/1rQ9qZPNoFGCfEndGOEbYLfsIET7TYuEf/view?usp=sharing



Hi,


I am working on a simple form with 5 text fields and a UIButton, particualry the the titleLabel.text property of the that button. What I am trying to do is to peform a check when the user hits the SignUp Button (see link for screenshot). So when the signup button is tapped, I want to run a check to see if:


A. Any of the text fields are empty (thinking about using the isEmpty property)

B. If the Button Label text is still "Tap to choose a role..." If the user selected a role by tapping the button, this text would be replaced by the role which confirms that the user selected a role.


When the check is peformed, if any combination of empty textfields and/or button labels are "invalid" (i.e empty or incorrect label), then I want to display a UIAlertController and the message parameter of the alert would show the textfields and/or button combos that need to be filled.


Any guidance on how to begin would be great, there are so many combinations, that could be possible ( 1 text fields, 2 textfields + button, just the button, etc). Here is my current code, but I dont wanna go down this route since that would be a lot of code writing and its probably not the best solution but a start...


// IBAction function for when the user taps sign up button
@IBAction func whenSignUpButtonIsTapped(_ sender: UIButton) {


     //Workflow
     // 1. Validation check
     // 2. Register User
      
     // 1 validation check...
      if (firstNameTextField.text?.isEmpty)!  {
         
         
         let missingInformationAlert = UIAlertController(title: "Missing Information", 
                                                        message: "First Name is required", 
                                               preferredStyle: .alert)
                
         let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
         
         
         missingInformationAlert.addAction(cancelAction)

         self.present(missingInformationAlert, animated: true, completion: nil)
         
         
      }


Thanks!

Accepted Reply

Thanks PBK and Claude31 and everyone else for the help on validating text inputs and keypad help...learned a lot of from this discussion 🙂. I decided to just implement a UIToolbar and make it the inputAccessoryView on the keypad and it works like a charm. Thanks again! I dont know what answer to put as the correct one since there is no one correct answer. I'm just putting my reply as one.

Replies

It is possible to add "manually" a return key, but a bit difficult to handle all the cases like rotation.


Another way is to dismiss by tapping anywhere outside.


For instance, adding a tap gesture to the view and resign first responder in tap IBAction


Or set the class of the UIView to UIControl (which is subclass of UIView)

Then set an action for event such as TouchDown ; resign first responder there.

Okay, I like that solution, but my question is will the user know to tap outside to dismiss, is it a pretty reasonable assumption from a User Interaction point of view given that iOS has been around for a long time?

Yes it is a reasonnable assumption on IOS. You can use it.

Another approach is to have your method dismiss the keyboard as soon as it detects that the phone number is a complete number. This is reasonable provided the user can tap phone number to edit if they entered a wrong number. When editing an already complete number your method should not immediately accept it - but rtaher allow the user to delete and re-enter one or more different digits.


type 609-555-1284

method accepts number and dismisses keyboard after typing the 4.

tap to edit

keyboard appears with cursor set after the 4

delete 4

delete 8

type 3

type 4

method accepts number and dismisses keyboard after typing the 4

Thanks PBK and Claude31 and everyone else for the help on validating text inputs and keypad help...learned a lot of from this discussion 🙂. I decided to just implement a UIToolbar and make it the inputAccessoryView on the keypad and it works like a charm. Thanks again! I dont know what answer to put as the correct one since there is no one correct answer. I'm just putting my reply as one.