Line data Source code
1 : import 'package:flutter/services.dart'; 2 : import 'package:folly_fields/util/mask_text_input_formatter.dart'; 3 : import 'package:folly_fields/validators/abstract_validator.dart'; 4 : 5 : /// 6 : /// 7 : /// 8 : class CreditCardExpirationValidator extends AbstractValidator<String> { 9 : /// 10 : /// 11 : /// 12 1 : CreditCardExpirationValidator() 13 1 : : super( 14 1 : <TextInputFormatter>[ 15 1 : MaskTextInputFormatter( 16 : mask: '##/##', 17 : ), 18 : ], 19 : ); 20 : 21 : /// 22 : /// 23 : /// 24 0 : @override 25 : String format(String value) => value; 26 : 27 : /// 28 : /// 29 : /// 30 0 : @override 31 : String strip(String value) => value; 32 : 33 : /// 34 : /// 35 : /// 36 1 : @override 37 : TextInputType get keyboard => TextInputType.number; 38 : 39 : /// 40 : /// 41 : /// 42 1 : @override 43 : bool isValid(String value) { 44 2 : value = value.replaceAll(RegExp(r'\D'), ''); 45 : 46 2 : if (value.length != 4) { 47 : return false; 48 : } 49 : 50 1 : String month = value.substring(0, 2); 51 : 52 1 : int? monthNum = int.tryParse(month); 53 2 : if (monthNum == null || monthNum < 1 || monthNum > 12) { 54 : return false; 55 : } 56 : 57 1 : String year = value.substring(2); 58 : 59 1 : int? yearNum = int.tryParse(year); 60 : if (yearNum == null) { 61 : return false; 62 : } 63 : 64 1 : yearNum += 2000; 65 : 66 1 : DateTime now = DateTime.now(); 67 : 68 3 : DateTime base = DateTime(now.year, now.month); 69 : 70 1 : DateTime expiration = DateTime(yearNum, monthNum); 71 : 72 1 : return expiration.isAfter(base); 73 : } 74 : }