title: Best practice for parsing and validating mobile number
body: <p>I wonder what the best practice for parsing and validating a mobile number before sending a text is. I've got code that works, but I'd like to find out better ways of doing it (as my last question, this is part of my early new years resolution to write better quality code!).</p>
  
  <p>At the moment we are very forgiving when the user enters the number on the form, they can enter things like "+44 123 4567890", "00441234567890", "0123456789", "+44(0)123456789", "012-345-6789" or even "haven't got a phone".</p>
  
  <p>However, to send the text the format must be 44xxxxxxxxxx (this is for UK mobiles only), so we need to parse it and validate it before we can send. Below is the code that I have for now (C#, asp.net), it would be great if anyone had any ideas on how to improve it.</p>
  
  <p>Thanks,</p>
  
  <p>Annelie</p>
  
  <pre><code>private bool IsMobileNumberValid(string mobileNumber)
      {
          // parse the number
          _mobileNumber = ParsedMobileNumber(mobileNumber);
  
          // check if it's the right length
          if (_mobileNumber.Length != 12)
          {
              return false;
          }
  
          // check if it contains non-numeric characters
          if(!Regex.IsMatch(_mobileNumber, @"^[-+]?[0-9]*\.?[0-9]+$"))
          {
              return false;
          }
  
          return true;
      }
  
      private string ParsedMobileNumber(string number)
      {
          number = number.Replace("+", "");
          number = number.Replace(".", "");
          number = number.Replace(" ", "");
          number = number.Replace("-", "");
          number = number.Replace("/", "");
          number = number.Replace("(", "");
          number = number.Replace(")", "");
  
          number = number.Trim(new char[] { '0' });
  
          if (!number.StartsWith("44"))
          {
              number = "44" + number;
          }
  
          return number;
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's what I ended up with:</p>
  
  <pre><code>private bool IsMobileNumberValid(string mobileNumber)
      {
          // remove all non-numeric characters
          _mobileNumber = CleanNumber(mobileNumber);
  
          // trim any leading zeros
          _mobileNumber = _mobileNumber.TrimStart(new char[] { '0' });
  
          // check for this in case they've entered 44 (0)xxxxxxxxx or similar
          if (_mobileNumber.StartsWith("440"))
          {
              _mobileNumber = _mobileNumber.Remove(2, 1);
          }
  
          // add country code if they haven't entered it
          if (!_mobileNumber.StartsWith("44"))
          {
              _mobileNumber = "44" + _mobileNumber;
          }
  
          // check if it's the right length
          if (_mobileNumber.Length != 12)
          {
              return false;
          }
  
          return true;
      }
  
      private string CleanNumber(string phone)
      {
          Regex digitsOnly = new Regex(@"[^\d]");
          return digitsOnly.Replace(phone, "");
      }
  </code></pre>