CRM 2013/2011 – Regex for JavaScript mobile phone validation

validating phone numbers is not a straight forward problem. One of the basic checks is to make sure it’s a certain length. Probably one of the hardest jobs is to work out exactly what your rules are for validation.

I think the best way to tackle this is use a JavaScript function and in that function you can do a number of checks

before I checked the validation of the number I would remove dashes and brackets and just concentrate on validating the number.

You could then validate the first part of the phone and check it’s an international code. If it wasn’t an international code then you could check it was a valid mobile

I would then use regular expression to check it was the a certain length

Sample code


function checkUKTelephone(telephoneNumber) {
/// <summary>
/// validates a uk mobile phone number
/// </summary>
// Convert into a string and check that we were provided with something
var telnum = telephoneNumber + " ";
if (telnum.length == 1) {
telNumberErrorNo = 1;
return false
}
telnum.length = telnum.length - 1;

// Remove spaces from the telephone number to help validation
while (telnum.indexOf(" ") != -1) {
telnum = telnum.slice(0, telnum.indexOf(" ")) + telnum.slice(telnum.indexOf(" ") + 1)
}

// Remove hyphens from the telephone number to help validation
while (telnum.indexOf("-") != -1) {
telnum = telnum.slice(0, telnum.indexOf("-")) + telnum.slice(telnum.indexOf("-") + 1)
}

// Remove hyphens from the telephone number to help validation
while (telnum.indexOf(")") != -1) {
telnum = telnum.slice(0, telnum.indexOf(")")) + telnum.slice(telnum.indexOf(")") + 1)
}

while (telnum.indexOf("(") != -1) {
telnum = telnum.slice(0, telnum.indexOf("(")) + telnum.slice(telnum.indexOf("(") + 1)
}

while (telnum.indexOf("+") != -1) {
telnum = telnum.slice(0, telnum.indexOf("+")) + telnum.slice(telnum.indexOf("+") + 1)
}

if (telnum.indexOf("44") == 0){
telnum = telnum.replace("44", "0");
}

var exp = /(070|071|072|073|074|075|076|077|078|079)\d{7,8}$/;
// Finally check that the telephone number is appropriate.
// exp = /(44|070|071|072|073|074|075|076|077|078|079)\d{7,8}$/;
if (exp != null) {
//if the regular expression is wrong I don't want to show the user, we can use F12 to debug
try {
if (exp.test(telnum) != true) {
telNumberErrorNo = 5;
return false;
}
}
catch (err) { }
}

// Telephone number seems to be valid - return the stripped telehone number
return telnum;
}
Advertisements

7 thoughts on “CRM 2013/2011 – Regex for JavaScript mobile phone validation

  1. Marko October 6, 2015 / 1:57 pm

    Hi Hosk,

    Why didn’t you use replace method to remove non-digits from tel number, instead of while loop?

    Cheers,

    Like

    • Hosk October 6, 2015 / 2:07 pm

      Probably because my REGEX skills are not very good, I was just happy I got it working

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s