Excel Formulas for Phone Number Masking and Validation
Organize the common Excel formulas for phone number masking, format validation, and test number generation. Explain how to write each formula, its applicable scenarios and boundaries, and provide a verification method before export, to help you process a column of phone numbers in bulk.
Key Points of This Article
- Use the REPLACE formula to replace the middle four digits with asterisks; this only changes the display and does not amount to encryption.
- The validation formula first checks whether the number has 11 digits, then checks whether the first digit is 1 and whether the second digit is between 3 and 9.
- The formula can only judge the format and cannot confirm whether a number is real and in use; it must be combined with business rules for handling.
- Randomly generated test numbers should be pasted as fixed values, and used only for testing, never for registration or impersonation.
Many people have a column of customer phone numbers. Before sharing it with colleagues, they need to mask the middle four digits; before importing it into the system, they need to filter out records with the wrong number of digits; and when preparing test data, they sometimes need to generate a batch of numbers on the fly. All three tasks can be done with Excel formulas, so there's no need to edit each one by hand.
This article covers three things: how to mask phone numbers with formulas, how to check whether a number's format is valid, and how to generate phone numbers for testing. For each type of formula, it explains the scenarios it suits and what it cannot do. After reading, you can apply the formulas directly to your own spreadsheets.
Phone number masking, validation, or generation: which formula suits you? One-sentence conclusion
First determine which type of problem you are solving, then choose the formula. Masking only changes how data is displayed, validation only produces a judgment result, and generation is only for testing; the three cannot substitute for one another.
| Your situation | Recommended Practices | Cause |
|---|---|---|
| I want to send the list to someone else, but I don't want to reveal the full numbers. | Use the REPLACE formula to replace the middle four characters, then paste the results as values and save as a new file. | Simple to operate, intuitive results; the original column does not need to be sent out. |
| Find the incorrectly formatted numbers before importing. | Validation formula using IF and AND, outputting "Format correct" or "Format incorrect" | Can determine the number of digits, the digit sum, and the leading digit of the number segment at once, for easy filtering |
| Need for system testing: a large amount of data that looks like mobile phone numbers | Generated with RANDBETWEEN, then pasted as values before use | Random results won't repeat a pattern, which makes them convenient for generating data in bulk. |
| Replace the numbers with identifiers that can be traced back to the originals. | Use sequence-number mapping with a separately stored lookup table | The formula itself cannot provide secure encryption; the lookup table is the basis for restoration. |
Before You Begin: Confirm These Points
1. Whether the phone number column is text or numbers. If the cells display in scientific notation, or the leading 0 of numbers starting with 0 disappears, the values have been treated as numbers. Formula results will then be wrong. It is recommended to first format the entire column as text, then paste the phone numbers again.
2. Check whether the number contains spaces, hyphens, or a country code. Phone numbers copied from web pages or other systems often include spaces, "-", or "+86". These characters can break digit-count checks, so clean the number first, then mask or validate it.
3. Check whether the table has header rows and blank rows. When a formula is copied down from the second row, a header or blank row in between will throw the results out of alignment. Take a look at the data range before you start.
4. Do you want masking or encryption? Masking only replaces digits with asterisks; the original text remains in the spreadsheet, so anyone who can open the file can still see it. Data that truly needs protection should have the original file stored separately, with access and sharing strictly limited.
5. Number segment rules change over time. At present, mainland China mobile numbers begin with 1, have 11 digits, and the second digit between 3 and 9 is more common. However, specific number segment allocations are subject to the latest regulations issued by the Ministry of Industry and Information Technology (MIIT). The rules in the formula are only suitable for preliminary screening and should not be treated as an absolute standard.
Steps to Mask the Middle Four Digits of a Phone Number Using a Formula
- Open the spreadsheet in Excel and confirm that the phone numbers are in column A, starting from row 2. If the numbers display incorrectly, first format the entire column as Text, then re-enter or paste them.
- In a blank column (for example, column B), enter the following formula in cell B2 to clean up spaces and dashes: =SUBSTITUTE(SUBSTITUTE(TRIM(A2)," ",""),"-",""). This step produces clean phone numbers.
- In cell C2, enter the masking formula: =REPLACE(B2,4,4,"\\\\"). It replaces 4 characters starting at position 4, so the first three digits and the last four digits are kept.
- Double-click the fill handle at the bottom-right corner of C2, or drag it down to the last row, to copy the formula downward.
- Check that the results follow the format "138\\\\0000". If an error value appears in a row, that row's phone number does not have 11 digits, so go back to the original data and check it.
- Select all results in column C, copy them, and use Paste Special > Values to paste them into a new column. This way, deleting column B or changing the original data will not turn the results into broken references.
- Save the work as a new file and send only the masked results. Keep the original plaintext spreadsheet in a separate secure location, and do not store it in the same file as the masked results.
Verify item by item after completion
After the desensitized results are generated, do not send them directly. Check each of the following points one by one; doing so can prevent most errors.
| Checklist items | How do you look at it? | What should I do if I have a problem? |
|---|---|---|
| Are the digit counts uniform? | Use the LEN function in a blank cell to count characters, or filter out rows whose length is not equal to 11. | Return to the original data, remove spaces and extra characters, then recalculate. |
| Is everything in the middle being replaced? | Randomly spot-check 5 to 10 rows to see whether the asterisks appear only in positions 4 to 7. | Check whether the REPLACE arguments were written as start position 4 and length 4. |
| Does plaintext remain? | Use the Find feature to search for the first three digits plus the last four digits of the original number. | Confirm that the result column has been pasted as values, and delete the formulas that reference the original column |
| Is there an error value? | Filter out the cells that start with the "#" symbol | Find the corresponding row, and determine whether it's an empty cell or the phone number has the wrong number of digits. |
| Does the file contain original columns mixed in? | Open the saved file and check for hidden columns and worksheets | Delete the hidden columns, or re-copy only the result columns to generate a new file. |
📊 Core Comparison
Phone number handling typically involves four kinds of needs, each requiring a different formula approach. The table below lists the formula for each and its notes.
| Requirements | Official notation (using A2 as an example) | Notes |
|---|---|---|
| Data masking display | ö=REPLACE(A2,4,4,"\\\\") | Only changes the display; the original value remains visible, it is not encryption |
| Format validation | =IF(AND(LEN(A2)=11,ISNUMBER(--A2),LEFT(A2,1)="1",MID(A2,2,1)>="3"),"Correct format","Incorrect format") | Only checks the number of digits, the digits themselves, and the leading digits of the number segment; it cannot confirm whether the number is actually in use. |
| Test Number Generation | ="1"&RANDBETWEEN(3,9)&TEXT(RANDBETWEEN(0,999999999),"000000000") | The result changes as the table refreshes; paste it as values to fix it. For testing only. |
| Substitution cipher | ="U"&TEXT(ROW()-1,"0000") | The numbering must be matched against a separately stored lookup table to map back to the original numbers; the formula itself cannot restore them. |
When using the validation formulas, there are two points to note. First, ISNUMBER(--A2) is used to determine whether the content can be converted into a number. If the ID number contains letters, the result will display "Format error." Second, MID(A2,2,1)>="3" compares the second character as text. It is suitable for a single digit and is not applicable for multi-digit comparisons.
When generating test phone numbers, also pay attention to their purpose. Randomly generated numbers may coincide with real numbers, so they must not be used to register accounts, send SMS messages, or impersonate others. When using them in a test system, it is recommended to label them "test data" in the file.
❓ Frequently Asked Questions (FAQ)
1. What is the difference between phone number masking and encryption?
Masking replaces some characters with asterisks for display, but the original data remains in the table, so anyone who can open the file can see the full number. Encryption requires specialized tools and keys, converting the content into a form that cannot be read directly. Excel formulas can only perform masking and ID substitution; they cannot replace encryption.
2. Why does a phone number display in scientific notation after I enter it?
Excel treats 11-digit numbers as numeric values, and when the cell is too narrow, they are automatically displayed in scientific notation. Setting the cell to text format before entering the number usually solves the problem. If the numbers have already been distorted, you need to re-import them from the original source.
3. Can a validation formula determine whether a number actually exists?
Not possible. The formula can only check the format, such as the number of digits and the leading digits of the number segment. Whether a number is actually in use or belongs to a particular carrier must be confirmed through a business system or an official verification channel.
4. Will the formula go wrong if the number has "+86" or spaces in front of it?
Yes. Numbers with "+86" have more than 11 digits, so the validation formula will flag them as invalid. Before validating, use SUBSTITUTE to remove "+86" and spaces, then run the check. If the dataset is large, you can first clean it up in bulk with Find and Replace.
📝 Summary
Phone number masking, validation, and generation each require formulas such as REPLACE, IF combined with AND, and RANDBETWEEN. Clean the data first, then apply the formulas, and finally check the results item by item to ensure the spreadsheet is usable. Masked results should be pasted as values and saved separately, while the original plaintext data is stored apart, so that personal information is protected while the data is being processed.


