iOS輸入數字驗證

驗證輸入值爲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;
  
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章