简体   繁体   English

用于验证手机号码的正则表达式

[英]Regular expression for validating a mobile number

I am using regular expressions to validate mobile numbers in Javascript. 我正在使用正则表达式来验证Javascript中的移动号码。

My conditions are: 我的条件是:

  1. The mobile number may start with +60 or +63 or +62 or +66 or 0 手机号码可以以+60或+63或+62或+66或0开头
  2. The mobile number have a length between 9 to 13 手机号码的长度在9到13之间

I have tried with the code: 我尝试过代码:

^(?:\+60|+63|+62|+66|0)[. ()-]*(?:\d[. ()-]*){10,11,12,13}$/;

But I didn't get it. 但我没有得到它。

The example mobile numbers are 示例移动电话号码是

+601126314441
01126342542
+631124445675
+661124445675
+621124445675
+60111243236
+62105273214
0105273214

Update your RegEx like so: 像这样更新您的RegEx:

/^(\+60|\+63|\+62|\+66|0)\d{9,13}$/gm

Verify at www.jsregex.com (don't forget to check the global and multiline options) 在www.jsregex.com上验证(不要忘记检查全局和多行选项)

Fiddle Example :) 小提琴示例:)

Javascript: 使用Javascript:

var reg = /(\+60|\+63|\+62|\+66|0)\d{9,13}/m;
var numbers = ['+601126314441', '01126342542', '+631124445675', '+661124445675', '+621124445675', '+60111243236', '+62105273214', '0105273214'];
var matched = [];
for (var i = 0; i < numbers.length; i++) {
    if (reg.test(numbers[i])) matched.push(numbers[i]);
}
console.log(matched.toString());
var testCases = [
    '+601126314441',
    '01126342542',
    '+631124445675',
    '+661124445675',
    '+621124445675',
    '+60111243236',
    '+62105273214',
    '0105273214'
]

function testMobNum (nstr) {
    return /^((\+{1}(60|62|63|66){1})|(0)){1}\d{9,13}$/.test(nstr);
}

testCases.map(testMobNum);
//returns [true, true, true, true, true, true, true, true]

You can use regexp from this example - 您可以使用此示例中的regexp -

$a = array(
'+601126314441',
'01126342542',
'+631124445675',
'+6611244-45675',
'+621124445675',
'+60111243236',
'+62105273214',
'01052-73214',
);

foreach($a as &$phone) {
    echo $phone . ' -> ';
    if (preg_match('/^(?:\+60|\+63|\+62|\+66|0)[0-9-]{9,13}$/', $phone)) {
        echo 'PASS';
    } else {
        echo 'FAIL';
    }
    echo PHP_EOL;
}

In php this will outputs following: 在php中,这将输出以下内容:

+601126314441 -> PASS
01126342542 -> PASS
+631124445675 -> PASS
+6611244-45675 -> PASS
+621124445675 -> PASS
+60111243236 -> PASS
+62105273214 -> PASS
01052-73214 -> PASS

if code length include to full mobile number you can try 如果代码长度包含完整的手机号码,您可以尝试

/^(\+6[0236]\d{7,11})|(0\d{8,12})$/

if + also include in number length then if +还包括数字长度

/^(\+6[0236]\d{6,10})|(0\d{8,12})$/

else try 别的尝试

/^(\+6[0236]|0)\d{9,13}$/

Hmm interesting :) 嗯有趣:)

Java Script Code: Java脚本代码:

// your regex pattern to check mobile number
var mobRegex = /^(\+60|\+63|\+62|\+66|0)\d{9,13}$/;
// empty array
var validMobile = [];
// some sample numbers to check
var mobileCollection = [
    '+60123456789',
    '+691234567891',
    '+6012345678912',
    '+60123456789123',
    '+601234567891234',
    '+63123456789',
    '+631WE234567891',
    '+6312345678912',
    '+63123456789123',
    '+6312 34567891234',
    '+62123456789',
    '+621234567891',
    '+6212345678912',
    '+62123456789123',
    '+6212-34567891234',
    '+66123456789',
    '+661234567891',
    '+6612345678912',
    '+66123456789123',
    '+661234OP7891234',
    '0123456789',
    '01234567891',
    '+9112445678912',
    '0123456789123',
    '01234567891234'
];
// check every number and file right one
for (var i = 0; i < mobileCollection.length; i++) {
    if (mobRegex.test(mobileCollection[i])) {
        validMobile.push(mobileCollection[i]);
    }
}
// alert all valid mobile number
alert(validMobile.toString());
// here is output
+60123456789,+6012345678912,+60123456789123,+601234567891234,+63123456789,
+6312345678912,+63123456789123,+62123456789,+621234567891,+6212345678912,
+62123456789123,+66123456789,+661234567891,+6612345678912,+66123456789123,
0123456789,01234567891,0123456789123,01234567891234 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM