[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1593
  • Last Modified:

How to validate UK telephone no

I am trying to create a validation function for UK telephone no.
I had created one but I am not sure what are the exact rules i should follow
the information available on internet is very ambiguous
For example if the length must be either 10 or 11 then what if the phone no also include country code or +44

Thanks
public static bool IsValidTelephone(string phoneNo)
        {
            bool rtnValid = true;
 
            phoneNo =  phoneNo.Replace("(", "").Replace(")", "").Replace("-", "").Trim();
 
            if (phoneNo.Length == 10 | phoneNo.Length== 11)
            {
 
                if (!Regex.Match(phoneNo, @"^0\d{2,4}[ -]{1}[\d]{3}[\d -]{1}[\d -]{1}[\d]{1,4}$").Success)
                {
                    return false;
                }
            }
            else
            {
                rtnValid = false;
            }
            return rtnValid;
        }

Open in new window

0
shieldguy
Asked:
shieldguy
1 Solution
 
deepu chandranCommented:
0
 
shieldguyAuthor Commented:
I had found a code in javascript but not sure if it will be good enough
please suggest
function checkUKTelephone (telephoneNumber) {
 
  // 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;
  
  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // 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)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
	
	// Disallow numbers allocated for dramas.
	 
  // Array holds the regular expressions for the drama telephone numbers
  var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
    if ( tnexp[i].test(telnum) ) {
      telNumberErrorNo = 5;
      return false;
    }
	}
  
  // Finally check that the telephone number is appropriate.
  exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";

Open in new window

0
 
waltersnowslinarnoldCommented:
Use the following to code, that will help you out..

 if (phoneNo.Length == 10 | phoneNo.Length== 11)
            {
 
                if (!Regex.Match(phoneNo, @"^[0]{1}\d{2,4}\s|-{1}\d|\s|-{6,9}$").Success)
                {
                    return false;
                }
            }


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
shieldguyAuthor Commented:
can you please tell me the rules its following so i can test it as well

Thanks
0
 
deepu chandranCommented:
hi,
You do not need that also, You can use RegularExpressionValidator  Control for that. That will do automatically in the client side.

Refer the blow link
http://msdn.microsoft.com/en-us/library/ms998267.aspx

Regards
Deepu
0
 
jeyan10Commented:
Hi,
   Try this Regular expression

Regex ukphone=new Regex(^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$);

if(!ukphone.IsMatch(txtukphone.txt)
{
  return false;
}
0
 
abelallenCommented:
Hi,

try the following function within your form

function isUKPhone3(data) {
return /(^(\+44)?[0-9\s\-]{10,})$/gi.test(data);

 // telephone number, only digits, space, hyphen and plus sign (to start with) allowed. Minimum of 10 characters. If + is the first character must be followed by 44.
}




0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now