-
Notifications
You must be signed in to change notification settings - Fork 66
Validating Phone (and fax) Numbers
Phone numbers are used for a number of models in PETS, including for all people types (AgencyPerson, CompanyPerson, JobSeeker) as well as for Agency and Company. The latter two models also have a 'fax' field, which is validated as a phone number. (note that the phone number for the People classes is part of, and validated by, the User model).
For this discussion, it should be noted that we are validating phone numbers as a convenience to the user, and are trying to detect incorrect numbers from being entered. At this time, we are not actually validating the that the number is a working phone number via an external API. Nor are we intending to use the number in an auto-dialer scheme. Also, we are not accommodating international numbers nor country codes.
Currently (July 10, 2016) our phone (and fax) number validator is too restrictive. We need to revise it to make sure that all of our design intent for the phone field is accommodated. In particular, these criteria should be addressed:
-
The use of an optional leading '1-' to indicate a long distance number. Some people find it useful to add this to a phone number as a reminder that the number is long-distance. This also allows for Canadian numbers to be stored (Canadian numbers can be called from the US by first entering '1' and then the phone number (which is in the same format as US numbers)). Thus, this would be a valid phone number:
1-555-123-4567
.
-
Update Sep. 9, 2016 - we also need to accommodate a leading '1' without a following dash, thus this would also be a valid phone number:
1555-123-4567
, etc.
-
Adding an office extension. We can limit this to a pattern that starts with an 'x', followed immediately (no spaces) by one or more digits. This then is a valid phone number:
(555)-222-3434 x1234
. This would not be a valid number:(555)-222-3434 x 1234
, nor would this:(555)-222-3434 x1 2 3 4
.
The current validator does not allow for the use of a period to separate number groups. So, 555.123.4567
is not a valid number. Unless the client indicates otherwise, we don't need to change this.
The current valuator works well for 10-digit phone numbers, with one exception (described below). In particular, it allows for reasonable patterns, such as 5551112323
, (555)111-2323
, (555)-111-2323
, (555) 111 2323
, and similar. It does not allow for more than one space between number groups, thus 555 111 2323
is not valid. All of these are OK.
The exception mention above is the use of parentheses around the first group of three numbers (that "area code"). Unmatched parentheses are not detected. This number is accepted as valid: (555-111-2323
, as is this: 555) 111 2323
.
In summary, the phone validator needs to be enhanced to:
- Allow for optional long-distance prefix,
- Allow for optional office extension,
- Detect mis-matched parentheses for area code.
It also needs to provide an informative error message to the user as to what format is acceptable and/or what part of the entered phone number is not valid.
Test Patterns
The Faker gem does a pretty good job of providing valid phone number patterns. For instance, this output (from rails console) provides good valid test data:
2.2.2 :050 > 20.times {puts Faker::PhoneNumber.phone_number}
1-846-608-2053 x724
1-872-928-5886
(797) 129-3692 x388
1-537-024-0525
314.704.8534 x255
383-516-5042
966-897-5114
(488) 194-7357 x73747
(887) 251-9348 x40017
(965) 890-8218 x13994
776.517.3128 x8058
1-910-123-9158 x2851
122.164.5820
894.971.0202
1-369-374-5803 x9579
(140) 761-4830
445.632.6171 x5573
1-182-672-3982
(924) 960-5845
833-638-6551 x16825
=> 20
All of these numbers are valid except for the ones where a period is used to separate number groups (e.g. 122.164.5820
). Additional example phone numbers that should be valid include:
(140)761-4830
(140)-761-4830
1-(140) 761-4830
11407614830