You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

美国手机号正则验证(禁止全零输入):测试有效但代码中不生效问题

Fixing All-Zero US Phone Number Validation Failure in .NET

Hey there, let's break down why your regex isn't blocking all-zero phone numbers in your .NET code, even though it worked in your test tool.

Your Current Setup

First, let's recap what you're working with. You have a model field with these validation attributes:

[Required(ErrorMessage = "Cell phone is required"), RegularExpression(Constants.PhoneRegex, ErrorMessage = "Invalid phone number")]
public string CellPhone { get; set; }

And your regex (meant to validate US numbers and block all zeros) is:

^(?!0+$)(\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4}))$

Your test cases show that 0000000000 is passing validation when it should fail, which is frustrating!

Why It's Failing

On paper, that (?!0+$) negative lookahead should block strings that are all zeros. But here's the catch:

  1. For unformatted all-zero numbers like 0000000000, if it's still passing, there might be a subtle issue in how .NET evaluates the regex (though the pattern should work). More likely, the bigger gap is that your original lookahead doesn't catch formatted all-zero numbers like (000) 000-0000—since those include non-zero characters (parentheses, hyphens), the 0+$ check doesn't trigger.
  2. Even if the unformatted case should work, a more reliable approach is to check for any non-zero digit in the number, regardless of formatting.

The Solution

Replace your regex with this version, which uses a positive lookahead to ensure at least one non-zero digit exists:

^(?=.*[1-9])(\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4}))$

What this does:

  • (?=.*[1-9]): This lookahead scans the entire string to confirm there's at least one digit from 1-9. No matter how the number is formatted (parentheses, hyphens, spaces), if all digits are zero, this check fails.
  • The rest of the regex stays intact to enforce the standard US phone number structure (3-digit area code, 3-digit exchange, 4-digit line number with optional formatting).

Testing the Fix

Let's run through your test cases (plus a formatted all-zero case) with this new regex:

Phone NumberActual ResultExpected Result
4561237890truetrue
1234falsefalse
0000000000falsefalse
(000) 000-0000falsefalse

Now all all-zero variations are blocked, and valid numbers still pass as expected.

Quick Checks If It Still Doesn't Work

If you're still seeing issues, double-check:

  • You've updated Constants.PhoneRegex correctly (no typos, and using the verbatim @ string to preserve backslashes in C#).
  • Your input strings don't have unexpected leading/trailing whitespace (if they do, add \s* at the start and end of the regex: ^\s*(?=.*[1-9])(\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4}))\s*$).
  • No other code is overriding or bypassing the validation logic.

内容的提问来源于stack exchange,提问作者Milind Mohapatra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:53:13