简体   繁体   中英

RegEx for phone number, all zeros not allowed

Need your help for RegEx. My current RegEx is /^\\d{8,}$/ - minimum length is 8, no letters, special characters and spaces allowed. I would also like to disallow all zeros like 00000000

Thanks!

This pattern should meet your needs: ^(?!0+$)\\d{8,}$

The (?!0+$) portion is a negative lookahead that will prevent an input of just zeros.

Example:

var pattern = @"^(?!0+$)\d{8,}$";
var inputs = new[]
{
    "00000000",         // false
    "(123) 456-789",    // false
    "123",              // false
    "01234567",         // true
    "500000000",        // true
    "123456789"         // true
};

foreach (var input in inputs)
{
    Console.WriteLine("{0}: {1}", Regex.IsMatch(input, pattern), input);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM