how to check that if text fileds values and show alert and empty the textfield in iphone

Loading...

how to check that if text fileds values and show alert and empty the textfield in iphone

I am entering value textfiled i want that if it is correct value then its ok other wise show alert and make the textfield empty.So that user may enter correct value i have tried many ways but i am not getting it solved.
  -(void)textFieldTextDidChangeClinicMarkup:(UITextField*)tf{

   NSString*test=clinicMarkupTextField.text;



if([test isEqualToString:@"1"]){
    NSString*value=@"1";

    appDelegate.clinicalMarkup=value;


}

else if([test isEqualToString:@"1.5"]){

    NSString*value=@"1.5";

    appDelegate.clinicalMarkup=value;


}


else if([test isEqualToString:@"2"]){

    NSString*value=@"2";

    appDelegate.clinicalMarkup=value;


}


else if([test isEqualToString:@"2.5"]){

    NSString*value=@"2.5";

    appDelegate.clinicalMarkup=value;


}


else if([test isEqualToString:@"3"]){

    NSString*value=@"3";

    appDelegate.clinicalMarkup=value;


}

else if([test length] >=3 || [test floatValue] > 3 || [test floatValue] <1 )  {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Clinic Markup can only be 1, 1.5, 2, 2.5 and 3 " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

     }
  }

Solutions/Answers:

Answer 1:

For this particular problem, I would use a UIStepper controll instead:

stepper.minimumValue = 1.0f;
stepper.maximumValue = 3.0f;
stepper.stepValue = 0.5f;

A stepper control looks like this:
enter image description here

Answer 2:

@james If you have only a single text field , then you can implement this code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
 NSString *strText=textField.text;
    if ([strText isEqualToString:@"1"]||[strText isEqualToString:@"1.5"]||[strText isEqualToString:@"2"]||[strText isEqualToString:@"2.5"]||[strText isEqualToString:@"3"]) 
    {
        appDelegate.clinicalMarkup=strText;
        [textField resignFirstResponder];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Clinic Markup can only be 1, 1.5, 2, 2.5 and 3 " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
return YES;
}

Answer 3:

Setup your delegate

clinicMarkupTextField.delegate = self;
clinicMarkupTextField.returnKeyType = UIReturnKeyDone;

and try the below code

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    if(clinicMarkupTextField == textField) //only if u have more then 1 text field
    {
        NSString*test=textField.text;

    if([test isEqualToString:@"1"]){
            NSString*value=@"1";
        appDelegate.clinicalMarkup=value;
     }

         ---SNIP---             

        else if([test length] >=3 || [test floatValue] > 3 || [test floatValue] <1 )  {

         //if all condition fails 
         textField.text = @"";

             UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Clinic Markup can only be 1, 1.5, 2, 2.5 and 3 " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alert show];
             [alert release];

          }
       }
 return yes;

    }

References

Loading...