Phone validation regex

后端 未结 16 1519
孤城傲影
孤城傲影 2020-11-27 02:56

I\'m using this pattern to check the validation of a phone number

^[0-9\\-\\+]{9,15}$

It\'s works for 0771234567 and +0

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 03:34

    I have a more generic regex to allow the user to enter only numbers, +, -, whitespace and (). It respects the parenthesis balance and there is always a number after a symbol.

    ^([+]?[\s0-9]+)?(\d{3}|[(]?[0-9]+[)])?([-]?[\s]?[0-9])+$

    false, ""
    false, "+48 504 203 260@@"
    false, "+48.504.203.260"
    false, "+55(123) 456-78-90-"
    false, "+55(123) - 456-78-90"
    false, "504.203.260"
    false, " "
    false, "-"
    false, "()"
    false, "() + ()"
    false, "(21 7777"
    false, "+48 (21)"
    false, "+"
    true , " 1"
    true , "1"
    true, "555-5555-555"
    true, "+48 504 203 260"
    true, "+48 (12) 504 203 260"
    true, "+48 (12) 504-203-260"
    true, "+48(12)504203260"
    true, "+4812504203260"
    true, "4812504203260
    

提交回复
热议问题