explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

Note to Self: Phone Number to String formatting

Leave a comment

Yep… still doing as3 and flex.

So the issue is this: The current database table that stores a phone number basically has a column for area code, a column for the phone number prefix and then a column for the suffix. Each has a 5 digit limit. This is an enterprise-wide table scheme for storing their phone numbers, so making a db change is unlikely to happen.

As for making the service modification to send the client a string…. well that’s a topic for another post.  The short of it is that I am parsing the above 3 fields coming in and aggregating them into a string value.  Here’s how (since the logic is kinda wonky).

           
[Deprecated]
public var phArea:int;

[Deprecated]
public var ph1:int;

[Deprecated]
public var ph2:int;

[Transient]
[Bindable]
public function get phoneNoString():String
{
	var s0:String = phArea > 0 ? phArea.toString() : "";
	var s1:String = ph1 > 0 ? ph1.toString() : "";
	var s2:String = ph2 > 0 ? ph2.toString() : "";

	return s0 + s1 + s2;
}

public function set phoneNoString( value:String ):void
{
        //remove any non-digit characters, e.g. (###) ###-####
        var s:String = value ? value.replace( /[\D]/g, "" ) : "";

	phArea = int( s.slice( 0, Math.min( 3, s.length )));
	ph1 = s.length > 3 ? int( s.slice( 3, Math.min( 6, s.length ))) : 0;
	ph2 = s.length > 6 ? int( s.slice( 6, s.length )) : 0;
}

There may be more efficient ways to achieve this (say via RegExp) but this was my first quick and dirty attempt to fix this irritating requirement.

Leave a comment