Phone number format in Javascript

≯℡__Kan透↙ 提交于 2019-12-02 10:02:00
if (/^(?:(?:\+|00)33|0)\d{9}$/.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}

The regex to match it would be this

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

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

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)

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}$/

This is a regex that validates your examples:

/(\+|(00)|0)[0-9]{11}/.test(stringToTest)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!