Best practice for parsing and validating mobile number Best practice for parsing and validating mobile number asp.net asp.net

Best practice for parsing and validating mobile number


Use a regular expression to remove any non-numeric characters instead of trying to guess how a person will enter their number - this will remove all your Replace() and Trim() methods, unless you really need to trim a leading zero.

string CleanPhone(string phone){    Regex digitsOnly = new Regex(@"[^\d]");       return digitsOnly.Replace(phone, "");}

Alternatively, I would recommend you use a masked textbox to collect the # (there are many options available) to allow only numeric input, and display the input with whatever format you'd like. This way you're guaranteeing that the value received will be all numeric characters.


Check out QAS, it's a commercial solution.

They have email, phone and address validations.

http://www.qas.com/phone-number-validation-web-service.htm

We use their services for Address and Email (not phone) and have been satisfied with it.


@annelie maybe you can update your regular expression to a more powerful one. Check out this site here. It contains many expressions but I think one of the top 2 expressions in the site should be suitable to you.