UITextField Validation for iOS

We all have experienced it’s very time taking task to handle all validations for a single text field like empty validation,  characters limit etc and to manage them in a efficient way. It is very annoying to show validation alerts for each text field, so if you have so many text-fields in a form and each text field have average of 2 validations then user may get frustrated to get so much alert during filling the form.

To overcome this problem I have created an inherited class of “UITextField” called “TextFieldValidator” in which you just need to give them all the regex string that you want to validate in a particular textfield, no matter how many types of validation you want to perform you just need to give them in sequence. I’m sure this will save your lot of time in development and good news is that you can easily integrate “TextFieldValidator” into your previous application without putting much effort.

For full information please visit TextFeildValidatorDemo

Here are some screenshots

Img1         IMG_5323

IMG_5331         IMG_5332

IMG_5325         IMG_5324

For full information please visit TextFeildValidatorDemo

I hope this will reduce your lot of efforts for implementing validation stuff and thanks for reading.

Please do comments on it so we can make it more better.

Thank you.

54 thoughts on “UITextField Validation for iOS

  1. Hi Dhawal,
    Great Job!
    I tried to integrate your code with my App but I do not see the error popup message like “User name should be 3 – 10 characters”. I only see the red icon. Can you please help?

    • Hello Srikanth,

      Thanks for the appreciation. I guess you forgot to set “presentInView” outlet, this is actually let the texfield know about where to show the error popup.

      Let me know if it works…

  2. one small doubt…we didn’t put ant text in ant textfields and we click submit button the error image will coming and when we click that error message the text will coming it is hard of user know so when the user didn’t enter any text in textfields and click the submit button the error image will come directly with error label message and with out clicking the error image

  3. Hi, It seems very helpful for for validation and I have downloaded your app, but I could not run the app. It gives me error like
    clang: error: no such file or directory: ‘/Users/paidpaperadmin/Downloads/TextFieldValidator-master/TextFieldValidationDemo/TextFieldValidationDemo/TextFieldValidationDemo-Prefix.pch’
    clang: error: no input files

    Can you share with me working app. This is my mail id: nallamsetty.narasimha@yahoo.com

  4. Hi, great job, I have a question
    Does this works with uibutton title text? for example if my uibutton title is empty

  5. Hi dhawaldawar, first of all, great job! if you add up all the time we’ve saved you would become a millionaire.

    Well, my question, i got a IBOutlet TextFieldValidator *card, it validate different credits cards type (Visa, Mastercard, Dinners, …), first TextFieldValidator *card is created with default regex for VISA, but i got an action that change credit card type, for example to MasterCard, in that moment i do as follow to reset my outlet and add the new regex

    self.card = [[TextFieldValidator alloc] init];
    [self.card addRegx:[self.validation RegexForMASTERCARD] withMsg:[self.validation MsgForInvalidCreditCard]];

    but it keep validating regex for VISA.

    i`m new in iOS, can you help me to know what i`m doing wrong? any advice?


  6. hi @dhawaldawar,
    is there any workaround to show all the error messages at once on click of submit button without the user having to tap on the red images one by one.


  7. My app is crashing. The error is below:

    Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Unable to parse constraint format:
    Unable to interpret ‘|’ character, because the related view doesn’t have a superview

    What I need to do

  8. Nice job,.. I implemented this nicely into my code. But I wanted to add checking of the database for an existing email. I get where it should be done, but I am not sure how I should test to find out if the TextValidator object that I have is for the Email field? Any suggestions?

    • Hi, thank you very much for your appreciation. As I have not implemented code to handle validation externally but definitely you can handle this easily by doing some modification in the code.

      You just need to declare “showErrorIconForMsg” method in TextFieldValidator.h file which is responsible to show error icon and for hiding the error icon you can write new method that will set the rightView of textfield to nil.

      Hope it will help you 🙂

  9. Great!! Great!! Great!!
    Good Job!

    However, in my case I have some UITextFields within a Register Form in a ScrollView so Alerts are not shown correctly because UITextFields positions change. How can I resolve it?

    Thank again my friend!!

    • Thanks Nuts.

      Can you please elaborate your scenario, as you said UITexfields position is changing so when alerts are creating problem either on tap of error icon or during some animation?

  10. Hi, This is great work but I want to add Textfield programmatically how can I connect TextFieldValidator class to TextField, please help me Thanks..

  11. Correct way for iOS 6

    NSAttributedString *newstring=[[NSAttributedString alloc] initWithString:self.strMsg];

    size =[newstring boundingRectWithSize:CGSizeMake(fieldFrame.size.width-(PaddingInErrorPopUp*2), 1000) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;

    size=CGSizeMake(ceilf(size.width), ceilf(size.height));

    Thanks for this information dhalwaldawar.Great work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s