Phone, Zip, and Email Formatting, in the BO?

Phone, Zip, and Email Formatting, in the BO?

Old forum URL: forums.lhotka.net/forums/t/2800.aspx


ltgrady posted on Monday, April 30, 2007

We have an ASP.NET app.  We have several objects that use contact infromation.  Members, Users, Customers, Reps, etc..

Our database just stores the numbers for phone and zip codes and the business objects just save and retrieve those numbered values.  For formatting we do some client side scripting for validation and some stripping of formatting and setting formatting in the code behind.

 

We're considering doing this all in our business object.  For instance, phone numbers would be a string returned from the BO formatted differently depending on teh length of the numbers.  (555) 555-5555 for 10 digit number, 555-5555 for 7 digit numbers, 1+ (555) 555-5555 for 11 digit numbers.  Then the same for Zip codes.  55555-5555 for 9 digit zips, 55555 for 5 digit zips, and 6A3 A6A for six digit, Canadian zips.  Then when you set thse values we would strip out all characters except numbers and digits before saving the values to the database.  We also were going to do some email validation as well, in the object, to make sure emails are formatted properly.

Is this on track, anyone have any good source/sample code or any articles that relate or deal with this.  I just want to check and see if anyone has a problem with this scenario or if i'm trying to re-create the wheel for something that's already a pretty standard business practice.  Thanks, lg.

SonOfPirate replied on Monday, April 30, 2007

The simplest way to accomplish this would be to define custom validation rules/methods for each property and encapsulate the necessary conditions and filters within them.  For instance, we have a PhoneNumberFormat rule method that uses the current user's culture to match a regular expression containing the formatting information for that region's phone numbers.  I can see doing the same for Postal Codes.

One more thing, we have the regular expressions used in the validation method exposed as static/shared fields in another class so that we can use them with the RegularExpressionValidator web control on the client to get better performance for the user.  Using the common expression, we know that validation will be the same in both places.

HTH

 

ajj3085 replied on Tuesday, May 01, 2007

Sounds good, but you should probably create a PhoneNumber object which has all the formatting rules.  It could work like the ToString method on number variables, where you can specify a format or just have the BO decide the format.

Now the question is.. are you supporting more than the US and Canada?  i wanted to do something similar, but the formats for numbers around the world vary so much and in some places change frequently, so it was quickly decided that it wasn't practical to do that.

SonOfPirate replied on Tuesday, May 01, 2007

Sorry, forgot that piece.  We keep the source for all of the expressions in resource files to make it easier to code and to change.  Our static/shared fields simply act as a public interface for the otherwise inaccessible resource file strings.

 

 

Copyright (c) Marimer LLC