1- import { Page , Observable , EventData } from '@nativescript/core' ;
1+ import { Page , Observable , EventData , TextField , PropertyChangeData } from '@nativescript/core' ;
22
33let page : Page ;
44
@@ -9,8 +9,71 @@ export function navigatingTo(args: EventData) {
99
1010export class SampleData extends Observable {
1111 textInput = '' ;
12- textChange ( args ) {
13- console . log ( args . object . text ) ;
14- this . notifyPropertyChange ( 'textInput' , args . object . text ) ;
12+ formattedSSNInput = '' ;
13+ formattedPhoneInput = '' ;
14+ valueFormatterSSN = formatSSN ;
15+ valueFormatterPhone = formatPhoneNumber ;
16+
17+ textChange ( args : PropertyChangeData ) {
18+ console . log ( args . value ) ;
19+ this . notifyPropertyChange ( 'textInput' , args . value ) ;
20+ }
21+
22+ textChangeSSN ( args : PropertyChangeData ) {
23+ console . log ( args . value ) ;
24+ this . notifyPropertyChange ( 'formattedSSNInput' , args . value ) ;
25+ }
26+
27+ textChangePhone ( args : PropertyChangeData ) {
28+ console . log ( args . value ) ;
29+ this . notifyPropertyChange ( 'formattedPhoneInput' , args . value ) ;
30+ }
31+ }
32+
33+ function formatPhoneNumber ( value : string , useParens ?: boolean ) {
34+ value = value . replace ( / \D / g, '' ) ;
35+ var size = value . length ;
36+ if ( useParens ) {
37+ if ( size > 0 ) {
38+ value = '(' + value ;
39+ }
40+ if ( size > 3 ) {
41+ value = value . slice ( 0 , 4 ) + ') ' + value . slice ( 4 , 11 ) ;
42+ }
43+ if ( size > 6 ) {
44+ value = value . slice ( 0 , 9 ) + '-' + value . slice ( 9 ) ;
45+ }
46+ } else {
47+ if ( size > 3 ) {
48+ value = value . slice ( 0 , 3 ) + '-' + value . slice ( 3 , 10 ) ;
49+ }
50+ if ( size > 6 ) {
51+ value = value . slice ( 0 , 7 ) + '-' + value . slice ( 7 ) ;
52+ }
1553 }
54+ return value ;
55+ }
56+
57+ function formatSSN ( value : string ) {
58+ // if input value is falsy eg if the user deletes the input, then just return
59+ if ( ! value ) return value ;
60+
61+ // clean the input for any non-digit values
62+ const ssn = value . replace ( / [ ^ \d ] / g, '' ) ;
63+
64+ // ssnLength is used to know when to apply our formatting for the ssn
65+ const ssnLength = ssn . length ;
66+
67+ // we need to return the value with no formatting if its less then four digits
68+ if ( ssnLength < 4 ) return ssn ;
69+
70+ // if ssnLength is greater than 4 and less the 6 we start to return
71+ // the formatted number
72+ if ( ssnLength < 6 ) {
73+ return `${ ssn . slice ( 0 , 3 ) } -${ ssn . slice ( 3 ) } ` ;
74+ }
75+
76+ // finally, if the ssnLength is greater then 6, we add the last
77+ // bit of formatting and return it.
78+ return `${ ssn . slice ( 0 , 3 ) } -${ ssn . slice ( 3 , 5 ) } -${ ssn . slice ( 5 , 9 ) } ` ;
1679}
0 commit comments