I am new to the ios development, I am just trying to create a simple application for calculations, So for that i have used to textboxes and i want to allow only the numeric values for these. i have already set the keyboard property to numeric pad. Now i want to do it programmatically for that i found the following function from checks for the String contains Numeric value only
+(BOOL) checkforNumeric:(NSString*) str
{
NSString *strMatchstring=@"\\b([0-9%_.+\\-]+)\\b";
NSPredicate *textpredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@", strMatchstring];
if(![textpredicate evaluateWithObject:str])
{
//////NSLog(@"Invalid email address found");
UIAlertView *objAlert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"please enter valid text." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close",nil];
[objAlert show];
[objAlert release];
return FALSE;
}
return TRUE;
}
Now i want to call it on button's click, i have tried some ways but it always shows error. Please help me how can i utilize this function in button's click.
My Actual code of button is:
-(IBAction)button{
if (firstValue.text.length>0 && secondvalue.text.length>0) {
label.text= [ [NSString alloc] initWithFormat:@"Result is: %.2f", ([firstValue.text floatValue]) * ([secondvalue.text floatValue])];
}
else if (firstValue.text.length==0 && secondvalue.text.length==0){
//label.text= @"please enter the values.";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Enetr First Value and second value." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}else if (firstValue.text.length==0) {
//label.text= @"please enter the first values.";
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Enter First Value" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}else if (secondvalue.text.length==0) {
//label.text= @"please enter the second values.";
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please Eneter Second Value" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Please help me.
I have tried the following ways for this:
BOOL *test= checkforNumeric(firstValue.text);