Owais Ahmed
Owais Ahmed

Reputation: 1438

Regex to validate numbers

Hi I am creating a regex to have:

Currently, I created

^(\+612)\d{8}$

Can anyone help me or suggest me how can I add the above validations in the regex.

Any help or suggestion would be appreciated.

Thanks in advance

Upvotes: 0

Views: 113

Answers (1)

revo
revo

Reputation: 48711

You need to have an alternation that starts matching at two points and do the replacement afterwards:

^ *(?:0 *2|\+ *6 *1 *2)(?: *\d){8} *$

Live demo

JS code:

if (/^ *(?:0 *2|\+ *6 *1 *2)(?: *\d){8} *$/.test(phoneNumber)) {
    phoneNumber = phoneNumber.replace(/^ *\+ *6 *1/, 0);
}

Upvotes: 3

Related Questions