我想构建一个可以验证来自不同国家/地区的数字的验证器(javascript),
我希望能够使用此骨架phone('NZ', '049234567', false, false);
是'COUNTRY PREFIX`,'NUMBER','LANDLINE','INTERNATIONAL'。
我将如何做这样的事情?
目前我正在考虑使用
之类的东西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]
]
}
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
World Telephone Number Guide对国家编号计划非常有用,尤其是例外情况。 ITU也有很多东西的免费标准。
保持与此信息保持同步可能很困难,因为各个国家/地区当局可能会在更新这些或其他类似网站之前进行“实时”更改。例如,WTNG上的新西兰入境目前有大约70个数字范围。
您最好的选择可能只是检查有效的国家/地区代码,区号的最小和最大位数(对于某些国家/地区可能是可选的,或者不是必需的)以及最小/最大长度。订户号码。
从UI的角度来看,我看到的一个好的布局有一个包含国家/地区名称和代码的下拉列表,然后是区分区域代码和用户号码文本字段。如果他们想要,用户还可以输入空格,破折号等;即用户总是对的: - )。
答案 2 :(得分:0)
我认为这个规则过于复杂,如果你这样做,你将不得不为所有内容编写单独的代码。
例如英国(来自this wikipedia page,为清晰起见删除了空格)
+4420xxxxxxxx London
+4429xxxxxxxx Cardiff
+44113xxxxxxx Leeds
+44116xxxxxxx Leicester
+44131xxxxxxx Edinburgh
+44151xxxxxxx Liverpool
+441382xxxxxx Dundee
+441386xxxxxx Evesham
+441865xxxxxx Oxford
+441204xxxxx Bolton
+4415396xxxxx Sedbergh
+4416977xxxx Brampton
请注意,其中一些长度不同,如果您想以规范格式吐出,那么对于那些等等,会有大量不同的间距要求。