Validating half life 2

10 Feb

The Number:: Phone:: Normalize Perl module could be a source of inspiration.I work for a market research company and we have to filter these types of input alllll the time. Just strip the non-alphanumeric chars, and see if there's an extension. The international accounts for an optional initial ' ' and country code. Valid matches would be: My gut feeling is reinforced by the amount of replies to this topic - that there is a virtually infinite number of solutions to this problem, none of which are going to be elegant. If you're talking about form validation, the regexp to validate correct meaning as well as correct data is going to be extremely complex because of varying country and provider standards. I interpret the question as looking for a broadly valid pattern, which may not be internally consistent - for example having a valid set of numbers, but not validating that the trunk-line, exchange, etc. North America is straightforward, and for international I prefer to use an 'idiomatic' pattern which covers the ways in which people specify and remember their numbers: The North American pattern makes sure that if one parenthesis is included both are. You'll have a hard time dealing with international numbers with a single/simple regex, see this post on the difficulties of international (and even north american) phone numbers. In my opinion, the most elegant solution is to validate a minimum length, nothing more.

You've correctly identified that it's a tricky problem...

How are extensions (e.g., 111-222-3333 x 44444) going to be handled? Even if you could write a big, hairy validator that would allow all the different legitimate formats, it would end up allowing pretty much anything even remotely resembling a phone number in the first place.

Here's a regex for a 7 or 10 digit number, with extensions allowed, delimiters are spaces, dashes, or periods: ^(? Honestly, I would recommend you don't try to validate phone numbers.

Although the answer to strip all whitespace is neat, it doesn't really solve the problem that's posed, which is to find a regex. '; my @tests = ( "1-234-567-8901", "1-234-567-8901 x1234", "1-234-567-8901 ext1234", "1 (234) 567-8901", "1.234.567.8901", "1/234/567/8901", "12345678901", "not a phone number" ); foreach my $num (@tests) # # Extract all phone numbers from an arbitrary file.

123 Validation Results Result from is Possible Number() true Result from is Valid Number() true Formatting Results E164 format 61299999999 Original format 61 2 9999 9999 National format (02) 9999 9999 International format 61 2 9999 9999 Out-of-country format from US 011 61 2 9999 9999 Out-of-country format from CH 00 61 2 9999 9999 Unless you are certain that you are always going to be accepting numbers from one locale, and they are always going to be in one format, I would heavily suggest not writing your own code for this, and using libphonenumber for validating and displaying phone numbers.