国际号码格式列表

发布于 2024-10-21 08:17:46 字数 2887 浏览 1 评论 0原文

我想要构建一个可以验证来自不同国家/地区的号码的验证器(javascript),

我希望能够使用这个骨架来调用它 phone('NZ', '049234567', false, false);

分别是“国家/地区前缀”、“号码”、“固定电话”、“国际”。

我该如何去做这样的事情呢?

目前我正在考虑有类似的东西

var phones = {
    NZ: {
        national: {
            landline: [
                '03{2,9}n(6)',  //read as 03[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '04{2,9}n(6)',  //read as 04[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '06{2,9}n(6)',  //read as 06[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '07{2,9}n(6)',  //read as 07[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '09{2,9}n(6)'   //read as 09[a number between 2 and 9][a random number][repeat previous expression 6 times]
            ],
            mobile: [
                '020n(7,10)', //read as 020[a random number][repeat previous expression between 7 and 10 times]
                '021n(7,10)', //read as 021[a random number][repeat previous expression between 7 and 10 times]
                '022n(7,10)', //read as 022[a random number][repeat previous expression between 7 and 10 times]
                '027n(7,10)', //read as 027[a random number][repeat previous expression between 7 and 10 times]
                '029n(7,10)'  //read as 029[a random number][repeat previous expression between 7 and 10 times]
            ]
        },
        international: {
            landline: [
                '+643{2,9}n(6)',  //read as +643[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+644{2,9}n(6)',  //read as +644[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+646{2,9}n(6)',  //read as +646[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+647{2,9}n(6)',  //read as +647[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+649{2,9}n(6)'   //read as +649[a number between 2 and 9][a random number][repeat previous expression 6 times]
            ],
            mobile: [
                '+6420n(7,10)',  //read as +6420[a random number][repeat previous expression between 7 and 10 times]
                '+6421n(7,10)',  //read as +6421[a random number][repeat previous expression between 7 and 10 times]
                '+6422n(7,10)',  //read as +6422[a random number][repeat previous expression between 7 and 10 times]
                '+6427n(7,10)',  //read as +6427[a random number][repeat previous expression between 7 and 10 times]
                '+6429n(7,10)'   //read as +6429[a random number][repeat previous expression between 7 and 10 times]
            ]
        }
    }
}

I am wanting to build a validator (javascript) that can validate numbers from different countries,

I want to be able to call it using this skeleton phone('NZ', '049234567', false, false);

which is 'COUNTRY PREFIX`, 'NUMBER', 'LANDLINE', 'INTERNATIONAL'.

How would I go about doing such a thing?

Currently I am thinking of having something like

var phones = {
    NZ: {
        national: {
            landline: [
                '03{2,9}n(6)',  //read as 03[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '04{2,9}n(6)',  //read as 04[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '06{2,9}n(6)',  //read as 06[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '07{2,9}n(6)',  //read as 07[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '09{2,9}n(6)'   //read as 09[a number between 2 and 9][a random number][repeat previous expression 6 times]
            ],
            mobile: [
                '020n(7,10)', //read as 020[a random number][repeat previous expression between 7 and 10 times]
                '021n(7,10)', //read as 021[a random number][repeat previous expression between 7 and 10 times]
                '022n(7,10)', //read as 022[a random number][repeat previous expression between 7 and 10 times]
                '027n(7,10)', //read as 027[a random number][repeat previous expression between 7 and 10 times]
                '029n(7,10)'  //read as 029[a random number][repeat previous expression between 7 and 10 times]
            ]
        },
        international: {
            landline: [
                '+643{2,9}n(6)',  //read as +643[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+644{2,9}n(6)',  //read as +644[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+646{2,9}n(6)',  //read as +646[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+647{2,9}n(6)',  //read as +647[a number between 2 and 9][a random number][repeat previous expression 6 times]
                '+649{2,9}n(6)'   //read as +649[a number between 2 and 9][a random number][repeat previous expression 6 times]
            ],
            mobile: [
                '+6420n(7,10)',  //read as +6420[a random number][repeat previous expression between 7 and 10 times]
                '+6421n(7,10)',  //read as +6421[a random number][repeat previous expression between 7 and 10 times]
                '+6422n(7,10)',  //read as +6422[a random number][repeat previous expression between 7 and 10 times]
                '+6427n(7,10)',  //read as +6427[a random number][repeat previous expression between 7 and 10 times]
                '+6429n(7,10)'   //read as +6429[a random number][repeat previous expression between 7 and 10 times]
            ]
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦旅人picnic 2024-10-28 08:17:46

世界电话号码指南对于国家编号计划非常有用,尤其是例外情况。 ITU 也为很多东西提供了免费的标准。

保持最新信息可能很困难,因为各个国家/地区当局可能会在这些或其他类似网站更新之前进行“实时”更改。例如,WTNG 上的 NZ 条目目前有大约 70 个号码范围。

您最好的选择可能只是检查有效的国家/地区代码、区号的最小和最大位数(对于某些国家/地区可能是可选的,或不是必需的)以及订户号码的最小/最大长度。

从用户界面的角度来看,我见过的一个好的布局有一个包含国家/地区名称和代码的下拉列表,然后是单独的区号和订户号码文本字段。如果用户愿意,还可以输入空格、破折号等;即用户永远是对的:-)。

The World Telephone Number Guide is quite useful for national numbering plans, especially the exceptions. The ITU has freely available standards for lots of stuff too.

Keeping up to date with this info may be difficult, since the various country authorities may make "live" changes before these or other similar sites are updated. For example, the NZ entry on WTNG currently has around 70 number ranges.

Your best bet is probably to just to check for valid country codes, a minimum and maximum number of digits for the area code (which may be optional, or not required, for some countries) and the min/max length for the subscriber number.

From a UI point of view, a good layout I've seen has a drop down list containing country names and codes, and then separate area code and subscriber number text fields. The user can also enter spaces, dashes, etc. if they want; i.e. the user is always right :-).

如梦初醒的夏天 2024-10-28 08:17:46

我认为这样做的规则将非常复杂,如果你这样做,你将不得不为所有内容编写单独的代码。

例如,对于英国(来自此维基百科页面,为了清晰起见,删除了空格)

+4420xxxxxxxx   London
+4429xxxxxxxx   Cardiff
+44113xxxxxxx   Leeds
+44116xxxxxxx   Leicester
+44131xxxxxxx   Edinburgh
+44151xxxxxxx   Liverpool
+441382xxxxxx   Dundee
+441386xxxxxx   Evesham
+441865xxxxxx   Oxford
+441204xxxxx    Bolton
+4415396xxxxx   Sedbergh
+4416977xxxx    Brampton

请注意,其中一些长度不同,如果您想以规范格式吐出,则这些内容有大量不同的间距要求,等等。

I think the rules for this are going to be prohibitively complex, and if you do this, you will have to write individual code for everything.

For example for the UK ( from this wikipedia page, with spaces removed for clarity )

+4420xxxxxxxx   London
+4429xxxxxxxx   Cardiff
+44113xxxxxxx   Leeds
+44116xxxxxxx   Leicester
+44131xxxxxxx   Edinburgh
+44151xxxxxxx   Liverpool
+441382xxxxxx   Dundee
+441386xxxxxx   Evesham
+441865xxxxxx   Oxford
+441204xxxxx    Bolton
+4415396xxxxx   Sedbergh
+4416977xxxx    Brampton

Note that some of those are different lengths, and if you want to spit out in the canonical format, there are tons of different spacing requirements for those, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文