2016-06-21 15 views
1

クレジットカード情報を入力していますが、有効期限を5文字以内にしたい4つの数字と最初の2文字の後に "/"が自動的に入力されます。つまり、01を入力すると01/17が自動的に入力されます。たった2文字 "17"。有効期限を考慮してテキストフィールドを設定するにはどうすればよいですか?

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
if textField == expire{ 

    } 
    return true 
} 
+0

使用これを:http://stackoverflow.com/questions/7709450/uitextfield-format-in-xx-xx-xxx – Vickyexpert

答えて

2

その私の最後で働いて、次のように試してみてください。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
    //Range.Lenth will greater than 0 if user is deleting text - Allow it to replce 
    if range.length > 0 
    { 
     return true 
    } 

    //Dont allow empty strings 
    if string == " " 
    { 
     return false 
    } 

    //Check for max length including the spacers we added 
    if range.location >= 5 
    { 
     return false 
    } 

    var originalText = textField.text 
    let replacementText = string.stringByReplacingOccurrencesOfString(" ", withString: "") 

    //Verify entered text is a numeric value 
    let digits = NSCharacterSet.decimalDigitCharacterSet() 
    for char in replacementText.unicodeScalars 
    { 
     if !digits.longCharacterIsMember(char.value) 
     { 
      return false 
     } 
    } 

    //Put/space after 2 digit 
    if range.location == 2 
    { 
     originalText?.appendContentsOf("/") 
     textField.text = originalText 
    } 

    return true 
} 

希望すると助かります。

0

このコードを試してみてください。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
    let num:NSString = textField.text! ; 
    if num.length == 1 
    { 
     textField.text = NSString(format:"%@%@/",num,string) as String 
     return false; 
    } 
    if num.length >= 5 { 
     //  allows only two more characters "17". 
     return false; 
    } 
    return true; 
} 
+0

これは迅速に行うことは可能ですか? – manatee

+0

はい、可能です。 –

0

回答がバックスペースを本当にうまく処理しません。ここに私の解決策は、(残念ながらObjective-Cで実装されている)だ:Jigarの答え@に基づいて

@interface CreditCardViewController() 
<UITextFieldDelegate> 
@property (weak,nonatomic) IBOutlet UITextField * cardExpirationTextField; 
@property (strong,nonatomic) NSString * previousExpiryDate; 
@property (strong,nonatomic) UITextRange * previousExpiryDateSelection; 
@property (assign,readonly,nonatomic) NSString * minYearLast2Digits; 
@end 

@implementation CreditCardViewController 

-(instancetype) init 
{ 
    self = [super initWithNibName:NSStringFromClass([CreditCardViewController class]) bundle:nil]; 
    if(self){ 
     _minYearLast2Digits = [[[NSNumber numberWithInteger:[NSDate date].year] stringValue] substringFromIndex:2]; 
    } 
    return self; 
} 

-(void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupView] 
} 

-(void) setupView 
{ 
    self.cardExpirationTextField.delegate = self; 
    [self.cardExpirationTextField addTarget:self 
           action:@selector(reformatCardExpiryDate:) 
         forControlEvents:UIControlEventEditingChanged]; 
} 

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 

    NSUInteger newLength = [textField.text length] + [string length] - range.length; 

    if(textField == self.cardExpirationTextField){ 
     self.previousExpiryDate = textField.text; 
     self.previousExpiryDateSelection = textField.selectedTextRange; 
     return newLength <= 5; 
    } 
    return YES; 
} 

-(void) reformatCardExpiryDate:(UITextField*) textField 
{ 
    const BOOL isErasing = self.previousExpiryDate.length > textField.text.length; 

    BOOL invalid = [textField.text length] > 5; 

    if([textField.text length] > 0){ 
     unichar firstChar = [textField.text characterAtIndex:0]; 
     invalid |= (firstChar > '1'); 
    } 
    if([textField.text length] > 1){ 
     unichar firstChar = [textField.text characterAtIndex:0]; 
     unichar secondChar = [textField.text characterAtIndex:1]; 
     invalid |= (firstChar == '1' && secondChar > '2'); 
    } 
    if([textField.text length] > 2){ 
     invalid |= [textField.text characterAtIndex:2] != '/'; 
    } 
    if([textField.text length] > 3){ 
     unichar yearFirstDigit = [textField.text characterAtIndex:3]; 
     unichar minYearFirstDigit = [self.minYearLast2Digits characterAtIndex:0]; 
     invalid |= yearFirstDigit < minYearFirstDigit; 
    } 
    if([textField.text length] > 4){ 
     NSString* yearLastTwoDigits = [textField.text substringFromIndex:3]; 
    invalid |= [yearLastTwoDigits compare:_minYearLast2Digits] == NSOrderedAscending; 
    } 

    if(invalid){ 
     [textField setText:self.previousExpiryDate]; 
     textField.selectedTextRange = self.previousExpiryDateSelection; 
     return; 
    } 

    if(!isErasing && textField.text.length == 2){ 
     textField.text = [textField.text stringByAppendingString:@"/"]; 
     UITextPosition *targetPosition = 
     [textField positionFromPosition:[textField beginningOfDocument] 
           offset:textField.text.length]; 

     [textField setSelectedTextRange: 
     [textField textRangeFromPosition:targetPosition 
           toPosition:targetPosition] 
     ]; 
    } 

} 

@end 
0

スウィフト4溶液、:いくつかの変更を加えた

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    if range.length > 0 { 
     return true 
    } 
    if string == "" { 
     return false 
    } 
    if range.location > 4 { 
     return false 
    } 
    var originalText = textField.text 
    let replacementText = string.replacingOccurrences(of: " ", with: "") 

    //Verify entered text is a numeric value 
    if !CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: replacementText)) { 
     return false 
    } 

    //Put/after 2 digit 
    if range.location == 2 { 
     originalText?.append("/") 
     textField.text = originalText 
    } 
    return true 
    } 
関連する問題