user1392981
user1392981

Reputation: 27

how to show alert when user enter other then number value in textfield

I have iphone app in which i enter any number value like 10 2 0 1 0.2 etc i want that instead of this if user enter any text it should alert that enter a number.

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Value Must Be In Number " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

Upvotes: 0

Views: 972

Answers (4)

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

You validate the entered character and do your logic in this delegate ,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

// your logic to restriction goes here.like check the entered char and through alert
}




-(BOOL) validateNumericValue:(NSString*) textValue
{   
    NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    NSNumber* number = [numberFormatter numberFromString:textValue];

    if (number != nil) 
    {
        return YES;
    }
    return NO;
}

Upvotes: 0

Dev
Dev

Reputation: 390

try this code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField==txtMobileNo) 
    {
        [self validatePhone];
    }else
    {
        [textField resignFirstResponder];

    }
    // called when 'return' key pressed. return NO to ignore.
    return YES;
}

- (BOOL) validatePhone
{
    NSString *phoneRegex = @"^+(?:[0-9] ?){6,14}[0-9]$"; 
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex]; 
    if ([phoneTest evaluateWithObject:txtMobileNo.text] == YES) 
    {
        NSLog(@"proper phone nO ");        
        return YES;   
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Please,Enter Your Person proper phone no" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil  ];
        [alert show];
        [alert release];
        NSLog(@"phone no. not in proper format");
        return NO;
        [txtMobileNo becomeFirstResponder];

    }  
}

I hope you helpful.

Upvotes: 1

Abhishek Singh
Abhishek Singh

Reputation: 6166

you can check if it is a number using the following:-

NSString *yourString= @"121212";
NSCharacterSet *decimalNUmSet= [NSCharacterSet decimalDigitCharacterSet];
BOOL isNum= ([[yourString stringByTrimmingCharactersInSet:decimalNUmSet] isEqualToString:@""] || 
                      [[yourString stringByTrimmingCharactersInSet:decimalNUmSet] isEqualToString:@"."]);

here the second part includes decimal numbers also.

the other way is to present a numeric keyboard to user so that he can type only numbers.

Upvotes: 0

Mario
Mario

Reputation: 4520

That disrupts the app kind of, doesn't it? What you want to look at is the UITextfield delegate methods. Set your viewController as the textields delegate and make it respond o UITextFieldDelegate protocol.

I would implement the textField:shouldChangeCharactersInRange:replacementString method and check the replacementString for unwanted characters.

But rather than displaying an alert, I guess I would just return NO if unwanted characters are contained there. Then, when the user enters something other than what you want, nothing happens.

Upvotes: 0

Related Questions