Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Masking a phone number behaves differently on iOS and Android.

$
0
0

I'm a little embarrassed.

I am having trouble with textfield behavior between iOS and android.

It seems that the mask that I have prepared for iOS is not working for android because the textfield in android will type in reverse whenever I modify it's contents, or put 1's interspersed whenever overtyping occurs.

Here is the code:

function maskPhoneNumber( phoneNumber, deletingText ) {
    //bandaid temporary fix for small bug that disallows deleting upon entering only 3 characters
    //the brilliant solution is to not mask it until there are 5 characters
    if( phoneNumber.length <= 4 ) return phoneNumber;
 
    //assure deletingText has a value as it is optional
    deletingText = deletingText || false;
 
    //take care of the edge case in which one character removed and added leaves deletingText true
    //deleting text was really only included anyway to take care of bugs occurring around or below 6 characters
    if( phoneNumber.length == 11 ) deletingText = false;
 
    //assure that it is a string
    var stringyPhone = String(phoneNumber);
 
    //refresh the number by stripping it of odd characters
    stringyPhone = stringyPhone.replace( /[^0-9]/g, '' );
 
    //return only the first 11 digits (if it starts with a 1)
    if (stringyPhone.slice( 0, 1 ) == '1' ) {
        stringyPhone = stringyPhone.slice( 0, 11 );
    } else {
        stringyPhone = stringyPhone.first( 0, 10 );
    }
 
    //if not deleting text, format it
    if (!deletingText && !Alloy.Globals.android ) {
        stringyPhone = stringyPhone.replace(/^([1]?)(\d{3})/, "1 ($2) "); //behavior requested by jason
        stringyPhone = stringyPhone.replace(/(\d{3})(\d{1,4})$/, "$1-$2");
    }else if (!deletingText && Alloy.Globals.android ) {
        stringyPhone = stringyPhone.replace(/^([1]?)(\d{3})(\d{3})(\d{4})$/, "1 ($2) $3-$4");
    }
 
    return stringyPhone;
}
Works without a hitch on iOS, any tips for android?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>