How to do validation on textfield? - iphone

I am making a registration form, in which I have 8 text fields and one submit button.
Whenever the user fails to enter one of the text fields, upon click of submit button an error message should be generated.
And when the user fills all the text fields, on click of submit button it should go to the next page.
Please give me some advice, thanks.

Simple logic, in your Submit action check that your textField.text is not nil or empty (#""). If not textField.text show UIAlert. Like this
-(IBAction) submitButton
{
if(self.txtName == nil || [self.txtName.text isEqualToString:#""])
{
[self showErrorAlert];
}
if(self.txtEmail == nil || [self.txtEmail.text isEqualToString:#""])
{
[self showErrorAlert];
}
}
// and show error alert as
-(void) showErrorAlert
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#""
message:#"All Fields are mandatory." delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}

-(void)emptyTextfieldVaildation
{
if( ([TxtFieldName.text isEqualToString:#""]) || ([TxtFieldPaswrd.text isEqualToString:#""]) )
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Error!!"
message:#"Please fill in the details." delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
[ErrorAlert release];
}
else
{
// Action to be called on Submit button touch
}
}

Set the method on click of submit button
-(IBAction)submit:(id)sender
{
//Do all the textField Validation
}
Check the all textfield data by using
[textfield.text isEqualToString:#""]
put this into if statement for all text field.. If condition is true , then show alert View

You have to check all fields before, and there are several way to do this. You can control the lenght of text of each field and show an alert if some field is empty. You can check the length property of the NSString, so you can also operate on the length(e.g the password must be 8 char, otherwise Alert.).
If you have tagged these 8 textFields(or if you know that there are just this textfields in your view) a good way may be like this:
for(UITextField * tf in [self.view subviews]){
if(![tf.text length]>0){
//show the alert
}
}
hope this helps.

Last night I struggled with this too and I used
if ([myTextField.text length] == 0) {
// error code to handle empty text field.
}
to handle empty text field. It works, for empty text field, of course, but failed if there are some spaces in there. Some people in SO give suggestion to trim the string first before evaluate using the code above. See link
How to enable a UIButton if a textfield is not empty?

set tag property of all your text fields uniquely and access them using if(textFieldName.tag==*yourtag*) in button action event...

Related

AlertView with textfield - beginner

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"HELOOOOOOOO" message:#"write a number of text-lines/paragraph here"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 95, 260, 25)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
If you look close you will find that the message attribute is quite long. I need this alertview to first display the title, and then my long message, and then the text field and finally the 2 buttons.
But what hapence here, is that since the message is too long, the textfield overlaps with the buttons.
How can i solve this ?
If this is for iPhone, you really can't. The UIAlertView is not meant to handle input. Extended messages are shown with a scroll view but you really should not add a UITextField to it's view hierarchy (for one, it goes against their design standards and your app MAY get nixed!).
In this situation, it would probably be best to use a new UIViewController to handle showing your content.
Again, the only "actions" that UIAlertView is meant to provide is that of the multiple buttons.
Implement this piece of code in your implementation file.
- (void)willPresentAlertView:(UIAlertView *)alertView
{
alertview.frame = CGRectMake(12, 95, 260, 25); //You can set a frame that suits to your needs
}
This delegate - (void)willPresentAlertView:(UIAlertView *)alertView will invoke before showing the alerview, so it is a better way to set frame to your alertview.
For more informations on aleert views:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intf/UIAlertViewDelegate
Actually, it doesn't matter how long your message is. The alert view will always size so it fits.
One way you can fix this is by adding a bunch of \n at the end of your message string, which is equivalent to putting a line break.
EDIT: If your message is so long that it puts it in a UIScrollView, there's nothing you can really do unless you're fine with major hacking (aka, changing the bounds of the UIAlertView, moving each button down, etc.).
A second way works only on iOS 5 and newer. You can set the UIAlertView's alertStyle property to UIAlertViewStylePlainTextInput which will display an alert view with a text field.
Use newline or line break character \n for this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"AlertView"message:#"\nhello:.................... \nwrite here.................. \nwrite here....................." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
Check this blog post for more help

How can i test if a certain table view cell has a certain image?

In my app, you can add and delete cells. I only want the user to be able to add 1 of each cell. So, is there a way to test if a cell already has a certain image?
like:
if(myTableView.cell.imageView == #"image.png"){
// do something
}
Please help! THanks
EDIT
This button allows us to add the cells and the images to the new cells.
- (IBAction)outlet1:(id)sender {
if (cart.cell.tag == 1) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:#"You have already added this" message:#"Go to My Cart and add more if you like" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else {
[cart.cells addObject:#"1"];
[[cart.cells lastObject] setTag:1];
UIImage * myImage = [UIImage imageNamed:#"paddle1.png"];
[cart.imageArray addObject:myImage];
}
}
so i basically want to test if this cell has been added already!
If you only have a few different images than you don't need to check for an image, rather you should set a tag number to the cell that corresponds to an image using
[cell setTag:(int)]; so paddle2.png can be 2 and paddle1.png can be 1 and you would [cell setTag:1] for paddle1.png . Than when testing simply check the tag:
If ([cell tag] == 1) {
//do something
}
here is the code for your button:
- (IBAction)outlet2:(id)sender {
[cart.cells addObject:#"1"];
[[cart.cells lastObject] setTag:2];
UIImage * myImage = [UIImage imageNamed:#"paddle2.png"];
[cart.imageArray addObject:myImage];
}
and here is the test code:
if(myTableView.cell.tag == 2){
// do something
}
As far as I know you cannot check for an image name, but you can keep an array of the images names that are stored in the cells. Then you can check the index of the row and the index of the image array names to see if it is what you are looking for:
if ([[[myimages objectAtIndex:i] valueForKey:#"ImageName"] isEqualToString:#"image.png"])
{
// you got what you were looking for,
}
'i' above would be the indexPath.row value - I am assuming that you are check this on the delegate method didSelectRowAtIndexPath.

how to set validation on button current title

hi i am new in iphone development,and i have task to develop the uibutton and picker relational application.
i have to develop the one view which have the button 1 uipicker view. when i run the application at that time the title of the button is blank. if the user directly click to submit the button at that time user will get the error message.
but i can get the error msg and my following code is not working it is submiting the form without checking the button text validation.
following is my code:
if ([btnPicker.currentTitle isEqualToString:#" "]) {
UIAlertView *alertPicker = [[UIAlertView alloc] initWithTitle:#"Required" message:#"Please,Select Month & Date." delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No",nil];
[alertPicker show];
NSLog(#"%#", alertPicker);
[alertPicker release];
}
[btnPicker.currentTitle isEqualToString:#""];
or
if ([btnPicker.currentTitle length]==0)
Actually a blank string is an empty string #"" not a space #" ". You have to test an empty string like this,
[btnPicker.currentTitle isEqualToString:#""];
Shouldn't it be [btnPicker.currentTitle isEqualToString:#""] ? String without the space?

disable Alert view button

I have an alert view for twitter posting.
Alert view has 2 button and a textfield
send and cancel
I want to disable send button, until user fills the message box(i.e textfield).
like,empty field kind of validation.
How can I disable send button?
I had a similar requirement and was able to do this without resorting to anything explicitly prohibited by Apple (ie, the use of private classes or API's). In the example below, I find and then disable the "Recover" button.
Note #1 -- The placement of "[alert Show]" is important. It (apparently) lays out the views, so must be done before attempting to look through the view hierarchy.
Note #2 -- the "contains:" method is one I defined that does an NSString case-insensitive substring search. Use rangeOfString perhaps in your code.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Application Warning"
message:#"What should I do with the file?"
delegate:self
cancelButtonTitle:#"Ignore"
otherButtonTitles:#"Remove", #"Recover", nil];
[alert show];
// try to find and disable "Recover" button
for(UIView *aView in alert.subviews)
{
if ([[[aView class] description] contains:#"Button"])
{
UIButton *aButton = (UIButton *)aView;
if ([aButton.titleLabel.text contains:#"Recover"])
{
aButton.enabled = NO;
}
}
}
This is not possible with the current SDK. You will have to create a custom view to take the user's input. The fact you are adding a textfield to the UIAlertView is itself unsupported and could break in any future SDK anyway.
I would suggest you create a custom view and if you still want it to look like a UIAlertView you can do this with appropriate images and custom buttons.

making a text field required in objective-c

I'm currently in the process of developing an app for the iPhone. There is a screen which requires users to enter their data in text format but it can easily be skipped by simply clicking the 'submit' button. Is there a way I can make these text fields required?
Have you tried de-activating the button unless the text field has something in it?
u can tell the button process to keep an eye on the text field so that if the text field is empty, the button wont process the function it would be like this
-(IBAction) buttonPressed:(id) sender
{
//----this will process the button function if the textfield is not empty
if(textField.text != #"")
{
>>> do the process <<<
}
else
//----this will show an alert message when the user tries to click button without filling the textfield
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error"
message:#"please fill the information first"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}