[Javascript] using regular expression to clean a phone number field

Michael Dougherty Michael_Dougherty at PBP.com
Fri Mar 7 09:52:08 CST 2003


I would like to put clean phone numbers into my database.  While i could use
some routine in the SQL SP to clean the phone number string before it's
INSERTed, i think using a regular expression replace on the string coming
from the form would be a more elegant solution.  MY main problem is that I
don't have a good idea how to use regular expressions.

phoneFormatUnknown = document.all["Phone"].value

phoneFormatOnlyNumbers = cleanerFunction(phoneFormatUnknown)

so that regardless of how the user enters the phone, only numbers end up in
phoneFormatOnlyNumbers

ex: (123) 123-1234		==> 1231231234
ex: 123.123.1234 			==> 1231231234
ex: 1(123) 123-1234 		==> 11231231234
ex: 1-123-123-1234 ext 123	==> 11231231234123
ex: 1-800-You're number 1	==> 18001 (OK that the output is as useless as the
input)

bonus: 1-800-OKCOMPAQ		==> 180065266722 (i don't care how ugly the regexp is
:)

the idea is specifically to NOT just nest a bunch of character-specific
replacements, or to iterate the string...




More information about the Javascript mailing list