java phone number validation

后端 未结 4 1855
無奈伤痛
無奈伤痛 2021-01-18 08:13

Here is my problem:

Create a constructor for a telephone number given a string in the form xxx-xxx-xxxx or xxx-xxxx for a local number. Throw an exception if the for

4条回答
  •  太阳男子
    2021-01-18 09:00

    ^(([(]?(\d{2,4})[)]?)|(\d{2,4})|([+1-9]+\d{1,2}))?[-\s]?(\d{2,3})?[-\s]?((\d{7,8})|(\d{3,4}[-\s]\d{3,4}))$
    

    matches: (0060)123-12345678, (0060)12312345678, (832)123-1234567, (006)03-12345678,

    (006)03-12345678, 00603-12345678, 0060312345678

    0000-123-12345678, 0000-12-12345678, 0000-1212345678 ... etc.

    1234-5678, 01-123-4567

    Can replace '-' with SPACE i.e (0080) 123 12345678

    Also matches +82-123-1234567, +82 123 1234567, +800 01 12345678 ... etc.

    More for house-hold/private number. Not for 1-800-000-0000 type of number

    *Tested with Regex tester http://regexpal.com/

提交回复
热议问题