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

时间:2013-12-30 08:54:28

标签: javascript jquery regex

我正在使用正则表达式来验证Javascript中的手机号码。

我的条件是:

  1. 手机号码可以以+60或+63或+62或+66或0
  2. 开头
  3. 手机号码的长度介于9至13之间
  4. 我尝试过代码:

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

    但我没有得到它。

    示例手机号码

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

5 个答案:

答案 0 :(得分:2)

像这样更新您的RegEx:

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

在www.jsregex.com验证(不要忘记检查全局和多行选项)

Fiddle Example :)

使用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());

答案 1 :(得分:1)

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]

答案 2 :(得分:0)

您可以使用此示例中的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;
}

在php中,这将输出以下内容:

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

答案 3 :(得分:0)

如果代码长度包含到完整的手机号码,您可以尝试

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

如果+还包含数字长度,则

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

其他尝试

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

答案 4 :(得分:0)

嗯有趣:)

Javascript代码:

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