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
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.
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…
Hi Dhawar,
Your code is working fine now. Can you tell me how can i display error after getting response from the API call?
its not coming..if u have came please send a code to the following mail id
Email Id: harish.sami08@gmail.com
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
You can call the tap action method under validation method of TextFieldValidator class if any error occurred
.
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
pch file is already included in the code, could you please download code again through url: https://github.com/dhawaldawar/TextFieldValidator
Still same error haunting me.. I could not fit it. Even though pch file is in the bundle, it gives me error to run the app. I have created a separate demo app with your code. I could run but validation is not happening. I don’t know where i am missing.
Finally I could do it dhawaldawar by following this link http://stackoverflow.com/questions/25840720/xcode-6-pch-file-not-found . Thank you so much for your work.
Great 🙂
Hi dhawaldawar,
I have an issue. When we click on image icon popup is being displayed with message. But in my app I have to display message when user tries to enter text in textfield. I have changed code according to my requirement. But the problem is
-(void)showErrorIconForMsg:(NSString *)msg{
UIButton *btnError=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btnError addTarget:self action:@selector(tapOnError) forControlEvents:UIControlEventTouchUpInside];
[btnError setBackgroundImage:[UIImage imageNamed:IconImageName] forState:UIControlStateNormal];
self.rightView=btnError;
self.rightViewMode=UITextFieldViewModeAlways;
strMsg=[msg copy];
[self showErrorWithMsg:msg];//Calling this method for showing popup when user tries to enter text in textfield.
}
Please check screen shots..
https://drive.google.com/file/d/0B1lZM8AQQVD8NjlCcm1TN1JscEk/view?usp=sharing
https://drive.google.com/file/d/0B1lZM8AQQVD8TGZ4UW14WUtiN2c/view?usp=sharing
When user clicks on image icon we are displaying popup with message. I need to implement exactly when user tries to enter text or when textfield is empty. Thank you.
There is a data member “validateOnCharacterChanged” in TextFieldValidator class, you just need to set the value to YES. This will handle your case that you want.
Hi dhawaldawar,
I have implemented validateOnCharacterChanged = YES. While entering text, validation is happening with message. But my problem is icon is not being displayed with message properly. I am not able to arrange popup exactly how we are displaying when user clicks on icon while entering text. Icon with message is being displayed differently while entering text.
May be you haven’t set correct view to presentInView outlet, make sure that you are giving superview reference of TextField to this outlet.
Hi, great job, I have a question
Does this works with uibutton title text? for example if my uibutton title is empty
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?
thanks!
Well i have found a simple solution, added a public method to remove all regex..
-(void)removeAllRegx{
[arrRegx removeAllObjects];
}
Great 🙂
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.
TIA
You can make “showErrorWithMsg:” method public in the .h file so using this method you can show message directly for every text field.
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
H:|-0-[popUp]-0-|
^’
What I need to do
For which iOS version you are getting this error?
Also please share some more information for that.
This happens if you dont set the presentInView property for the given TextField
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 🙂
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?
Thanks Nitish 🙂
Good one..
Happy coding..!!
Hi, This is great work but I want to add Textfield programmatically how can I connect TextFieldValidator class to TextField, please help me Thanks..
You don’t need to connect “TextFieldValidator” class to UITextField as this is inherited class of UITextField so you can use in the similar way as you create for UITextField, you just need to replace the class name by “TextFieldValidator”.
Grate work ..Thanks
Thanks 🙂
Really appreciate that… I used in xcode 6… 😉
Thanks 🙂
I think i have a small problem when adding uitextfields programmatically. The code crashes IQPopUp class drawRect method because color is NULL
const CGFloat *color=CGColorGetComponents(popUpColor.CGColor);
any suggestions? =) Thanks again
Thanks for finding this issue, I have resolved this and committed updates on git repository, you can download the same using URL https://github.com/dhawaldawar/TextFieldValidator
You said it. 7.0 “and above” 🙂 thank you so much!
is this available for ios8?
Great Work
Thanks Paras 🙂
Perfectly for my App – Thanks a lot!
Good to hear 🙂
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.
Thanks rajeev for shared the code 🙂
boundingRectWithSize for size(TextFieldValidator.m) is not working in ios6.
I have made it for iOS7.0 or above only.
You can change if you want to make compatible for iOS6.0
Good work man 😉
Thanks 🙂
great job
Great 🙂
A great start 🙂