add phone number validator

This commit is contained in:
Jack Flintermann 2016-02-19 20:32:24 -05:00
parent 85c0fda57a
commit bc5a1dd720
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,17 @@
//
// STPPhoneNumberValidator.h
// Stripe
//
// Created by Jack Flintermann on 10/16/15.
// Copyright © 2015 Stripe, Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface STPPhoneNumberValidator : NSObject
+ (BOOL)stringIsValidPhoneNumber:(nonnull NSString *)string;
+ (nonnull NSString *)formattedPhoneNumberForString:(nonnull NSString *)string;
@end

View File

@ -0,0 +1,36 @@
//
// STPPhoneNumberValidator.m
// Stripe
//
// Created by Jack Flintermann on 10/16/15.
// Copyright © 2015 Stripe, Inc. All rights reserved.
//
#import "STPPhoneNumberValidator.h"
#import "STPCardValidator.h"
#import "NSString+Stripe.h"
@implementation STPPhoneNumberValidator
+ (BOOL)stringIsValidPhoneNumber:(NSString *)string {
return [STPCardValidator sanitizedNumericStringForString:string].length == 10;
}
+ (NSString *)formattedPhoneNumberForString:(NSString *)string {
NSString *sanitized = [STPCardValidator sanitizedNumericStringForString:string];
if (sanitized.length >= 6) {
return [NSString stringWithFormat:@"(%@) %@-%@",
[sanitized stp_safeSubstringToIndex:3],
[[sanitized stp_safeSubstringToIndex:6] stp_safeSubstringFromIndex:3],
[[sanitized stp_safeSubstringToIndex:10] stp_safeSubstringFromIndex:6]
];
} else if (sanitized.length >= 3) {
return [NSString stringWithFormat:@"(%@) %@",
[sanitized stp_safeSubstringToIndex:3],
[sanitized stp_safeSubstringFromIndex:3]
];
}
return sanitized;
}
@end