UITextField validations [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi i am beginner in Ios in my project i am crating one registration page and there i have to provide validations for textfields as like best level and i have searched for this so many textfield validations tutorials but i did not get correct one
According to my requirement when i clicked on textfield if there is any error then need to show alert message on textfields like below screen for this i have written so many custom methods but they are not working please help me some one

You have to make your custom view for validate the textFields
You can try to do it with https://github.com/ustwo/US2FormValidator
Code in reference to Objective-C
US2ValidatorTextField *firstNameTextField = [[US2ValidatorTextField alloc] init];
firstNameTextField.validator = [[[MyProjectValidatorName alloc] init] autorelease];
firstNameTextField.shouldAllowViolation = YES;
firstNameTextField.validateOnFocusLossOnly = YES;
firstNameTextField.placeholder = #"Enter first name";
firstNameTextField.validatorUIDelegate = self;
[_textUICollection addObject:firstNameTextField];
[firstNameTextField release];
Hope it helps you.

I've written many UITextField validators and its always best to do this sort of thing yourself as each requirement is different and there is more control over it.
The best time to validate is normally when UItextField did end editing - this way you know the user is done.
You need to look at UITextFields delegate methods and use the text argument from there to check length and that sort of thing.
I can't supply any code as you haven't stated what sort of validation you want done. If its email - this is the sort of thing you need (Its in Objective-C though)
/* Email address validation test */
NSString *emailReg = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailReg];
if ([emailTest evaluateWithObject:emailAddress]){
validEmail = YES;
}

Its like you can implement this with simple code as like checking and showing an alert.
if the textfield is empty
if the textfield contains numbers
if the textfield is a email field
if the all the required text field is filled
you can achieve this by using this piece of code
// To check the email field is not empty string
if([[self.email text] isEqualToString:#""]){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Enter your email!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
// To check the email field has '#' empty string
else if ([self.email.text rangeOfString:#"#"].location == NSNotFound){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Enter proper email!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
you can implement like this or you can use libraries depending on your requirement.

Related

Tracking which UIAlert button is clicked to change UILabel

I'm a complete beginner and looking to learn how to start coding to build my own apps.
I've been messing around with the classic "Hello World" design and trying to improve on it based on the limited knowledge i have managed to gather in the past week but i've got stuck and hope someone can help me out!
So far I have a UITextField for the user to enter their name, a label to display their name and a button to update the display. To help understand UIAlertViews better I have also included a Alert if the name entered is Chris (or chris) and another alert if the name entered is not Chris. All this is functioning fine so far.
I'm looking to track what button is pressed on my incorrectName UIAlertView and then update my UILabel to say "Chris" if they click the "I Want to be Called Chris" button. I have build another app similar to this before from a tutorial and I have copied across the code which i thought should function the same but it doesnt seem to work.
Here is my .m file so far
#import "Solo1ViewController.h"
#interface Solo1ViewController ()
#end
#implementation Solo1ViewController
#synthesize setMessage;
#synthesize userName;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=([alertView buttonTitleAtIndex:buttonIndex]);
if ([buttonTitle isEqualToString:#"I Want To Be Called Chris!"])
{setMessage.text=#"Chris";}
}
-(IBAction)showMessage:(id)sender
{
setMessage.text=userName.text;
if ([userName.text isEqual:#"Chris"] || [userName.text isEqual:#"chris"])
{
UIAlertView *correctName;
correctName = [[UIAlertView alloc]initWithTitle:#":)" message:#"Great Name!" delegate:nil cancelButtonTitle:#"Bye Fellow Chris" otherButtonTitles:nil, nil];
[correctName show];
}
else
{
UIAlertView *incorrectName;
incorrectName=[[UIAlertView alloc] initWithTitle:#":(" message:#"Chris is a Better Name" delegate:nil cancelButtonTitle:#"No, Thanks" otherButtonTitles:#"I Want To Be Called Chris!", nil];
[incorrectName show];
}
}
-(IBAction)removeKeyboard:(id)sender{
[userName resignFirstResponder];
}
To me it seems as though this should work but I guess I'm probably missing something glaringly obvious or i'm doing it totally wrong.
Make sure to set the alert view's delegate to self! Right now you have the delegate set as nil.
Example:
incorrectName=[[UIAlertView alloc] initWithTitle:#":(" message:#"Chris is a Better Name" delegate:self cancelButtonTitle:#"No, Thanks" otherButtonTitles:#"I Want To Be Called Chris!", nil];

If else statement is causing me trouble [duplicate]

This question already has answers here:
How to use an UIActionSheet within an if statement? [closed]
(2 answers)
Closed 9 years ago.
I have an if else if statement that is giving me trouble.
First, here's my code:
if([_hasUserTakenAPhoto isEqual: #"YES"]) {
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:#"Delete" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
_xButtonActionSheetTitle = [_xButtonAfterPhotoTaken buttonTitleAtIndex:0];
} else if ([_xButtonActionSheetTitle isEqualToString:#"Delete"]) {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
} else {
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
}
My problem is that I need to perform the method call of:
[_xButtonActionSheetTitle isEqualToString:#"Delete"] and if it is equal to "Delete", then I need to perform a segue using this method call:
[self performSegueWithIdentifier:#"backToHomeFromMediaCaptureVC" sender:self];
The only way I know how to do this is with another if statement, which you can see in my original code I started with "else if", but this creates different scope for all of my action sheet statements.
I just need to make the title of "Delete" on my action sheet tappable and it needs to trigger the segue.
I was trying to follow this action sheet answer here: https://stackoverflow.com/a/17374248/3117509
I just can't seem to figure this out.
Closing question. The problem is that I do not have a solid grasp on UIActionSheets.

iOS: How do I get text from a text field and display it in an alert?

guys. This is totally going to sound like I'm asking for you to do my homework for me, but I'm not. My employer FINALLY gave me this sweet new MacBook Pro. One of my tasks will include some iOS development. I'm pumped about it and I'm trying to dive right in to learning, so I'm making a silly little application that just lets me see how to interact and write some code. This morning's task is to take some text from a text field and display it in an alert. I've done lots of googling and found lots of things -- even stuff on StackOverflow -- but a lot of it is over my head or not exactly relevant. So, I'm hoping someone can show me what I've done wrong.
Here's my code for the text field:
-(IBAction)showInputMessage:(UITextField *)textField
{
if ([textField.text isEqualToString:#""])
{
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textField.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
And then I connect that text field to showInputMessage and run it in the iPhone simulator, but nothing happens when I enter text and click "Enter".
Thanks in advance. I've only been playing with this language since last night.
Jeremy
Set Delegate for UITextView.
First declare the delegate:
#interface YourViewController ()<UITextFieldDelegate>
Second set to self
self.textView.delegate = self;
Use this method:
-(void)textViewShouldReturn:(UITextField *)textField
{
if ([textField.text isEqualToString:#""]){
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textField.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
To add a property write the following code in your h file:
#property (weak, nonatomic) IBOutlet UITextView *textView;
To connect it your text field go to the storyboard and click its view controller. Next go to the Connections Inspector(All the way on the right). Under outlets drag the circle next to textView to your textView.
Have you linked the UITextField in the xib file or the storyboard?
I see now that you have a textField as a parameter. You won't get anything like that;
Do this in .h
IBOutlet UITextField *textFieldTest;
then link it in the xib or storyboard
-(IBAction)showInputMessage
{
if ([textFieldTest.text isEqualToString:#""])
{
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:#"Name!" message:[NSString stringWithFormat:#"Message: %#", textFieldTest.text]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}

iOS UIAlertview clear text and disable OK

I'm a bit new to iOS development, and right now am working on some simple UI-related stuff. I have a UIAlertView that I'm using at one point to allow the user to enter some text, with simple Cancel and OK buttons. The OK button should be disabled if the text field is blank.
I added to my UIAlertViewDelegate an alertViewShouldEnableFirstOtherButton function, so the OK button would disable when there's no text, and I also set the UIAlertView's UITextField to have clearOnBeginEditing true, so the previous text would be gone every time I displayed the alert. Each of these things works perfectly on their own. Unfortunately, it seems like the AlertView is checking whether or not to enable the OK button before the text field is cleared, so when they're put together it comes up enabled. Below should be about the minimal code needed to reproduce.
-(void)viewDidLoad
{
textEntryBox = [[UIAlertView alloc] initWithTitle:#"Name" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[textEntryBox setAlertViewStyle:UIAlertViewStylePlainTextInput];
[textEntryBox textFieldAtIndex:0].clearsOnBeginEditing = YES;
}
-(IBAction)functionTriggeredByOtherLogic
{
[textEntryBox show];
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if(alertView == textEntryBox)
{
if([[alertView textFieldAtIndex:0].text length] > 0)
{
return YES;
}
else
{
return NO;
}
}
return YES;
}
So, ultimately, my question is this: am I doing something completely against the natural iOS way of doing things here? Is there a better way to do this? Should I just ignore the clearsOnBeginEditing property of the UITextField, and manually clear the Text property before showing the UIAlertView?
Try to set the textfield delegate to self
[[textEntryBox textFieldAtIndex:0] setDelegate:self]
and implement this method :
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setText:#""];
}
I'm also having a UIAlertView with a textField to fill-in in my app, and it works for me
Using an alert view for this is probably a bit much. It might be easier if you use the master-detail paradigm and just push a new view controller where you can enter your values.

Ask user opinion, with custom UIAlertView, what's the limit to be approved?

I'm creating an app, and at one moment i want to ask the user opinion with a custom UIAlertView. After a lot of researches, and reading about this subject, i'm a little confused about some things...
What objects (UITextfields, UIImages...) could we add to an UIAlertView ?
Because i found this :
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
But this could have been accepted for example :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"msg" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[alertView addSubview:txtField];
[alertView show];
[alertView release];
(See this link for more informations)
So for example, if i don't want the blue-box with message, and title parameters, could i only do this code :
UIAlertView *alert = [[UIAlertView alloc] init];
[alert addSubview:a_UIButton];
[alert addSubview:a_UIImageview];
[alert show];
So with this sort of code i could get the advantage (if it's work !) of put in pause mode all the app', and all would be custom. But is it possible ? Could it be reject ?
Thanks a lot !
Instead of trying to customize "UIAlertView", why not just create your own custom AlertView (from "UIView") and add that as a subview when you want to display it?
We don't know if (or how) Apple might change the internal architecture of UIAlertView in future versions of iOS (e.g. iOS 7) that would break all the various customizations done to it in all the apps that have gotten away with it so far. That's why Apple put up that warning in their documentation.
I would recommend instead that you make a custom UIView, and just display that with a similar animation to the UIAlertView. Then, you can add whatever UI elements you'd like. If the docs say the view hierarchy is private, I'd leave it alone.
If all you want to do is add a text field to the alert view, that functionality is supported in iOS 5 and later using:
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];

Resources