Repository navigation
Expand file tree
/
Copy pathforms.ts
More file actions
79 lines (66 loc) · 2.09 KB
/
Copy pathforms.ts
File metadata and controls
79 lines (66 loc) · 2.09 KB
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
import { Page, Observable, EventData, TextField, PropertyChangeData } from '@nativescript/core';
let page: Page;
export function navigatingTo(args: EventData) {
page = <Page>args.object;
page.bindingContext = new SampleData();
}
export class SampleData extends Observable {
textInput = '';
formattedSSNInput = '';
formattedPhoneInput = '';
valueFormatterSSN = formatSSN;
valueFormatterPhone = formatPhoneNumber;
textChange(args: PropertyChangeData) {
console.log(args.value);
this.notifyPropertyChange('textInput', args.value);
}
textChangeSSN(args: PropertyChangeData) {
console.log(args.value);
this.notifyPropertyChange('formattedSSNInput', args.value);
}
textChangePhone(args: PropertyChangeData) {
console.log(args.value);
this.notifyPropertyChange('formattedPhoneInput', args.value);
}
}
function formatPhoneNumber(value: string, useParens?: boolean) {
value = value.replace(/\D/g, '');
var size = value.length;
if (useParens) {
if (size > 0) {
value = '(' + value;
}
if (size > 3) {
value = value.slice(0, 4) + ') ' + value.slice(4, 11);
}
if (size > 6) {
value = value.slice(0, 9) + '-' + value.slice(9);
}
} else {
if (size > 3) {
value = value.slice(0, 3) + '-' + value.slice(3, 10);
}
if (size > 6) {
value = value.slice(0, 7) + '-' + value.slice(7);
}
}
return value;
}
function formatSSN(value: string) {
// if input value is falsy eg if the user deletes the input, then just return
if (!value) return value;
// clean the input for any non-digit values
const ssn = value.replace(/[^\d]/g, '');
// ssnLength is used to know when to apply our formatting for the ssn
const ssnLength = ssn.length;
// we need to return the value with no formatting if its less then four digits
if (ssnLength < 4) return ssn;
// if ssnLength is greater than 4 and less the 6 we start to return
// the formatted number
if (ssnLength < 6) {
return `${ssn.slice(0, 3)}-${ssn.slice(3)}`;
}
// finally, if the ssnLength is greater then 6, we add the last
// bit of formatting and return it.
return `${ssn.slice(0, 3)}-${ssn.slice(3, 5)}-${ssn.slice(5, 9)}`;
}