驗證輸入值爲0~9之間一個數字
//數字驗證
-(BOOL) isNumber:(NSString*)number {
NSString *numberRegex = @"[0-9]";
NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
BOOL isMatch = [numberTest evaluateWithObject:number];
if (!isMatch) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"只能輸入一個數字" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
return NO;
}
return YES;
}
驗證輸入值爲數字
//數字驗證
-(BOOL) isNumber:(NSString*)number {
NSString *numberRegex = @"[0-9]+";
NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
BOOL isMatch = [numberTest evaluateWithObject:number];
if (!isMatch) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"只能輸入數字" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
return NO;
}
return YES;
}