Friday, December 9, 2011

iPhone how to check that a string is numeric only

static bool TextIsValidValue( NSString* newText, double &value )
{
bool result = false;

if ( [newText isMatchedByRegex:@"^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"] ) {
result
= true;
value
= [newText doubleValue];
}
return result;
}

- (IBAction) doTextChanged:(id)sender;
{
double value;
if ( TextIsValidValue( [i_pause stringValue], value ) ) {
[i_pause setTextColor:[NSColor blackColor]];
// do something with the value
} else {
[i_pause setTextColor:[NSColor redColor]];
}
}