How to validate a (country specific) phone number

前端 未结 8 628
一个人的身影
一个人的身影 2020-12-30 00:44

A valid phone number contains:

  • Less than 9 characters
  • A "+" at the start
  • Only digits.

I\'m trying to use regular expre

相关标签:
8条回答
  • 2020-12-30 01:02

    Your regex should look like this, you need information about char counter

    @"^(\+[0-9]{9})$"
    
    0 讨论(0)
  • 2020-12-30 01:15

    If you're looking for a country specific regex, try this expression which works for all Australian (+61-) numbers. I've put comments on how to go about varying it for other uses.

    public static bool IsValidPhoneNumber(string phoneNumber)
    {
        //will match +61 or +61- or 0 or nothing followed by a nine digit number
        return Regex.Match(phoneNumber, 
            @"^([\+]?61[-]?|[0])?[1-9][0-9]{8}$").Success;
        //to vary this, replace 61 with an international code of your choice 
        //or remove [\+]?61[-]? if international code isn't needed
        //{8} is the number of digits in the actual phone number less one
    }
    
    0 讨论(0)
提交回复
热议问题