Let the computer do the formatting
Brian Crescimanno recently published a form usability checklist on A List Apart. It's a great article that hits home some of my form pet peeves.
One of the points he makes is to let the computer do the formatting:
Few things confuse users as often as requiring that users provide information in a specific format....
Be reasonable; are we so afraid of regular expressions that we can't strip extraneous characters from a single input field? Let the users type their telephone numbers in whatever they please. We can use a little quick programming to filter out what we don?t need.
It is pretty easy to write a regular expression that takes out anything but numbers, you would do it like this in CFML:
<cfset form.phone = "(800) 555-1212"> <cfset cleanPhone = ReReplace(form.phone, "[^0-9]", "", "ALL")> <cfoutput>#cleanPhone#</cfoutput>
Now your left with a variable
cleanPhone that has just the phone number digits. So now you need to format the phone number yourself, this can also be done easily with the help of the Left, Right, and Mid functions in ColdFusion:
<cfset areaCode = Left(cleanPhone, 3)> <cfset firstThree = Mid(cleanPhone, 4,3)> <cfset lastFour = Right(cleanPhone, 4)> <cfoutput>(#areaCode#) #firstThree#-#lastFour#</cfoutput>
This example however does make the assumption that the phone number is a US phone number. Things can get a bit more tricky when you add international phone numbers, and extensions into the mix.
Like this? Follow me ↯Tweet Follow @pfreitag
Let the computer do the formatting was first published on December 22, 2005.
If you like reading about validation, form, usability, phone, formatting, or regex then you might also like:
- CFPARAM for Simple String Validation
- Howto build a form that isn't annoying (Part 1)
- Howto make Friendly URLs
- Keep in mind that users don't read
The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.