-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathCardTextBox.cs
223 lines (208 loc) · 7.34 KB
/
CardTextBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Text;
using Windows.UI.Xaml.Controls;
namespace Telegram.Controls.Payments
{
public partial class CardTextBox : TextBox
{
public string[] PREFIXES_15 = new[] { "34", "37" };
public string[] PREFIXES_14 = new[] { "300", "301", "302", "303", "304", "305", "309", "36", "38", "39" };
public string[] PREFIXES_16 = new[]
{
"2221", "2222", "2223", "2224", "2225", "2226", "2227", "2228", "2229",
"223", "224", "225", "226", "227", "228", "229",
"23", "24", "25", "26",
"270", "271", "2720",
"50", "51", "52", "53", "54", "55",
"4",
"60", "62", "64", "65",
"35"
};
public static int MAX_LENGTH_STANDARD = 16;
public static int MAX_LENGTH_AMERICAN_EXPRESS = 15;
public static int MAX_LENGTH_DINERS_CLUB = 14;
private string previousText = string.Empty;
private int selectionStart;
private int characterAction = -1;
private int actionPosition;
private bool ignoreOnCardChange;
public CardTextBox()
{
TextChanging += OnTextChanging;
TextChanged += OnTextChanged;
}
private void Started()
{
var start = SelectionStart;
var after = Math.Max(0, Text.Length - previousText.Length);
var count = Math.Max(0, previousText.Length - Text.Length);
if (count == 0 && after == 1)
{
characterAction = 1;
}
else if (count == 1 && after == 0)
{
if (previousText[start] == ' ' && start > 0)
{
characterAction = 3;
actionPosition = start - 1;
}
else
{
characterAction = 2;
}
}
else
{
characterAction = -1;
}
}
private void OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs e)
{
Started();
if (ignoreOnCardChange)
{
return;
}
int start = SelectionStart;
string str = Text;
if (characterAction == 3)
{
str = str.Substring(0, actionPosition) + str.Substring(actionPosition + 1);
start--;
}
StringBuilder builder = new StringBuilder(str.Length);
for (int a = 0; a < str.Length; a++)
{
char ch = str[a];
if (char.IsDigit(ch))
{
builder.Append(ch);
}
}
ignoreOnCardChange = true;
string hint = null;
int maxLength = 100;
if (builder.Length > 0)
{
string currentString = builder.ToString();
for (int a = 0; a < 3; a++)
{
string[] checkArr;
string resultHint;
int resultMaxLength;
switch (a)
{
case 0:
checkArr = PREFIXES_16;
resultMaxLength = 16;
resultHint = "xxxx xxxx xxxx xxxx";
break;
case 1:
checkArr = PREFIXES_15;
resultMaxLength = 15;
resultHint = "xxxx xxxx xxxx xxx";
break;
case 2:
default:
checkArr = PREFIXES_14;
resultMaxLength = 14;
resultHint = "xxxx xxxx xxxx xx";
break;
}
for (int b = 0; b < checkArr.Length; b++)
{
string prefix = checkArr[b];
if (currentString.Length <= prefix.Length)
{
if (prefix.StartsWith(currentString))
{
hint = resultHint;
maxLength = resultMaxLength;
break;
}
}
else
{
if (currentString.StartsWith(prefix))
{
hint = resultHint;
maxLength = resultMaxLength;
break;
}
}
}
if (hint != null)
{
break;
}
}
if (maxLength != 0)
{
if (builder.Length > maxLength)
{
builder.Length = maxLength;
}
}
}
if (hint != null)
{
if (maxLength != 0)
{
if (builder.Length == maxLength)
{
//inputFields[FIELD_EXPIRE_DATE].requestFocus();
}
}
//phoneField.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
for (int a = 0; a < builder.Length; a++)
{
if (a < hint.Length)
{
if (hint[a] == ' ')
{
builder.Insert(a, ' ');
a++;
if (start == a && characterAction != 2 && characterAction != 3)
{
start++;
}
}
}
else
{
builder.Insert(a, ' ');
if (start == a + 1 && characterAction != 2 && characterAction != 3)
{
start++;
}
break;
}
}
}
else
{
//phoneField.setTextColor(builder.Length() > 0 ? Theme.getColor(Theme.key_windowBackgroundWhiteRedText4) : Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
}
Text = builder.ToString();
if (start >= 0)
{
//phoneField.setSelection(start <= phoneField.Length() ? start : phoneField.Length());
selectionStart = start <= Text.Length ? start : Text.Length;
SelectionStart = selectionStart;
}
ignoreOnCardChange = false;
previousText = Text;
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
SelectionStart = selectionStart;
}
}
}