0

In my app I want to validate user inputs in the fields. For Example

If textfields is empty. Or email is in correct format

I don't want to add validation logic in ViewController, Neither I want to pass UIView to a Validator Class

I have multiple screens like this.

Any suggestion for best ways validating Screens.

Irfan
  • 5,070
  • 1
  • 28
  • 32
  • 1
    I would create a validator class for every field. and pass either the string from the field or the field self. Why don't you want to do that? – vikingosegundo Jan 25 '16 at 07:04

2 Answers2

2

what you can do is create category class of UIView and write instance method like

Validation+UIView

+(BOOL)isEmpty:(NSString *)string{
    // snippet to check empty validation and return value.
    }


+(BOOL)emailValidation:(NSString *)string{
// snippet to check email validation and return value.

}

Now use this instance method on your view.

if(![yourview isEmpty]){
// all good ...
}
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
1

If you are targeting it for multiple scenes then it is good to have a validator class for all your UITextFields. I have written a UITextField validator class which is very simple to use. You can modify it as you want. Take a look. Best luck

iAnurag
  • 9,286
  • 3
  • 31
  • 48
  • Only this class will be enough. All you have to do is just put this class as your all textField's superclass. – iAnurag Jan 25 '16 at 07:16