4

I am using regular expressions to validate mobile numbers in Javascript.

My conditions are:

  1. The mobile number may start with +60 or +63 or +62 or +66 or 0
  2. The mobile number have a length between 9 to 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
11
  • This might help stackoverflow.com/questions/15088518/… Commented Dec 30, 2013 at 8:57
  • Can you post an example of a number that should be matched, but isn't ? Or an example of a number that isn't matched, but should be? Commented Dec 30, 2013 at 8:57
  • Upated the Mobile numbers..@Guntram Blohm Commented Dec 30, 2013 at 9:00
  • 1
    @PravinKumar only this conditions? or something else like brackets? Commented Dec 30, 2013 at 9:01
  • 1
    so try this /(\+60|\+63|\+62|\+66|0)\d{9,13}/ Commented Dec 30, 2013 at 9:04

5 Answers 5

2

Update your RegEx like so:

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

Verify at www.jsregex.com (don't forget to check the global and multiline options)

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());
Sign up to request clarification or add additional context in comments.

4 Comments

With this regexp, the matched number will have between 11 and 15 numbers in it if it begins with +6[0236] and between 10 and 14 numbers if it begins with 0. You should modify the \d{9,13} part.
@Valentin possibly code length not included for mobile length
@Grundy Requirements aren't clear enough to be sure, that's true :)
Yeah I just went by what his original number was I'm sure he can tweak it to figure the correct numbers. Unfortunately I know nothing about these phone number assignments haha
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]

Comments

0

You can use regexp from this example -

$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:

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

Comments

0

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

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

else try

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

Comments

0

Hmm interesting :)

Java Script Code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.