Validating phone numbers in a form without writing a regular expression
Phone numbers are the field that goes wrong most often. A digit missing, a letter typed in, a number pasted with spaces, an international prefix written three different ways. If the list is meant for sending messages or calling people back, every bad number is a person you cannot reach, and you only find out when the message fails.
The standard fix is a pattern, also called a regular expression or regex, that says exactly what a valid number looks like. The trouble is that few people who build forms can write one. This article shows how to get a correct pattern without writing it, and what the pattern will and will not accept, so that you can decide whether it is right for your form.
Decide what you accept
Three questions settle most cases:
- Is it a national mobile number or any phone number in the world? A mobile number has a known start and a known length in each country. An international number varies.
- Do you need the country code? If the answers come from one country, a national format is simpler for people to type.
- Are spaces and dashes allowed? For the ready-made formats below, the answer is no: digits only. Say so in the question's help text so people know.
The ready-made formats
Next to the pattern box of a field, the Pattern helper offers formats for exactly this:
- Mobile number. Two settings: what it starts with and the total digits. Start with 07 and ten digits in total and the helper writes
^07[0-9]{8}$for you: 07 followed by exactly eight more digits. - International phone number. Two settings: the fewest and most digits, 8 and 15 by default, and an optional leading
+. It is written^\+?[0-9]{8,15}$. - Fixed-length number, when what you need is simply a number of a set length, and Digits only, when only the digits matter.
Pick one, adjust its settings, and press Use this. The pattern and an error message are filled in for you.
Test before you publish
In the same window, Try a value lets you type an answer and shows which formats accept it. Try the cases that will really happen:
| What you type | Mobile starting 07, 10 digits |
|---|---|
0791234567 |
accepted |
079123456 (one digit short) |
refused |
0791234567abc (letters after it) |
refused |
079 123 4567 (with spaces) |
refused |
The pattern has to match the whole answer, which is why the third line fails: a number followed by letters is not a valid number, even though it starts with one.
Three things to watch
Digits typed in Arabic numerals. The ready-made formats accept the digits 0 to 9. A number typed with Arabic-Indic digits (٠٧٩١٢٣٤٥٦٧) is refused, even though a person would read it as the same number. Whether this happens depends on the keyboard and the phone settings, but if your respondents might type the other kind, say in the help text, "write the number with digits 0 to 9".
Spaces and dashes. 079 123 4567 is refused too. Tell people to type the number without spaces, or, if you would rather be flexible, write your own pattern in the box.
Country rules. The formats are generic: you choose the starting digits and the length. Pulseform does not ship a list of national numbering plans, so check the right start and length for your country before you set them, for example with the telephone regulator or an operator. Then the pattern only needs to be as strict as the rule.
Where it is enforced
The pattern is checked on the public link, on the in-app fill page, and when you import a CSV file (a row with a bad number is skipped and reported). It is not checked in the Collect app, which field teams use without a connection, because an offline phone cannot judge patterns reliably. A collector's answer is always kept, and if the number does not fit it is marked so you can filter for those records and call back to fix them.
Using the pattern somewhere else
The patterns above are written in ordinary regular expression syntax, so you can copy one into any tool that accepts regular expressions. Test it there with the same values first, as tools differ in small ways. If you only want the pattern, our free pattern builder writes it for you without an account.
In Pulseform
See building a form and its field rules for the pattern box and the helper, and answers that do not fit the pattern for the records from the field. The wider picture is in form data validation.
Try it on your own data
Create a survey or form and see the results as answers arrive.
Start your 14-day free trial