Phone number format in Javascript

2019-03-05 08:20发布

I need to check Format Phone Number.

+33xxxxxxxxx

0033xxxxxxxxx

EDIT : 0xxxxxxxxx

How can I do that with(out) regex ?

5条回答
聊天终结者
2楼-- · 2019-03-05 08:56

Try this regex: /^((\+|00)\d{2})?\d{9}$/

This matches each of your given cases (+YYXXXXXXXXX, 00YYXXXXXXXXX, XXXXXXXXX).

Edit: To match your edit: /^((\+|00)\d{2}|0)\d{9}$/

查看更多
神经病院院长
3楼-- · 2019-03-05 09:08
if (/^(?:(?:\+|00)33|0)\d{9}$/.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}
查看更多
▲ chillily
4楼-- · 2019-03-05 09:13

This is a regex that validates your examples:

/(\+|(00)|0)[0-9]{11}/.test(stringToTest)
查看更多
霸刀☆藐视天下
5楼-- · 2019-03-05 09:19

PhoneFormat.com has a javascript library that has some formatting functions that you could easily drop into your project. It will take whatever number you throw at it and try and convert it to e164 (+ 33252525252), and also format it (+33 2 52 52 52 52)

查看更多
做个烂人
6楼-- · 2019-03-05 09:21

The regex to match it would be this

(0033|\+33|0)?\d{9}

I use http://regexpal.com/ for quick regex testing

查看更多
登录 后发表回答