Since we can't rely on a specific programming language syntax, like JavaScript, to talk about GraphQL schemas, we'll define our own simple language. GraphQL services can be written in any language. Then, when queries come in, they are validated and executed against that schema. But it's useful to have an exact description of the data we can ask for - what fields can we select? What kinds of objects might they return? What fields are available on those sub-objects? That's where the schema comes in.Įvery GraphQL service defines a set of types which completely describe the set of possible data you can query on that service. For the object returned by hero, we select the name and appearsIn fieldsīecause the shape of a GraphQL query closely matches the result, you can predict what the query will return without knowing that much about the server.Since you'd need a regex anyway, you might as well have the regex do all the work. Take, for instance, my test script that downloads a web page and extracts all phone numbers using the regex. 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.Īlthough the answer to strip all whitespace is neat, it doesn't really solve the problem that's posed, which is to find a regex. Google's version has code for Java and Javascript, but people have also implemented libraries for other languages that use the Google i18n phone number dataset: Libphonenumber also gives you many additional benefits, such as grabbing the location that the phone number is detected as being, and also getting the time zone information from the phone number: PhoneNumberOfflineGeocoder Resultsīut the invalid Australian phone number ( (09) 9999 9999) returns that it is not a valid phone number. Out-of-country format from CH 00 61 2 9999 9999 So not only do you learn if the phone number is valid (which it is), but you also get consistent phone number formatting in your locale.Īs a bonus, libphonenumber has a number of datasets to check the validity of phone numbers, as well, so checking a number such as +61299999999 (the international version of (02) 9999 9999) returns as a valid number with formatting: Validation Results Out-of-country format from CH 00 1 23 ext. Using one of your more complex examples, 1-23 x1234, you get the following data out of libphonenumber (link to online demo): Validation Results I would suggest skipping a simple regular expression to test your phone number against, and using a library such as Google's libphonenumber (link to GitHub project). (09) 9999 9999 is not a valid Australian numberĪ regular expression is fine for checking the format of a phone number, but it's not really going to be able to check the validity of a phone number.(02) 9999 9999 is also a valid Australian number.0404 999 999 is a valid Australian number.The biggest problem with phone number validation is it is very culturally dependant. PhoneNumberOfflineGeocoder - provides geographical information related to a phone number.findNumbers - finds numbers in text input.AsYouTypeFormatter - formats phone numbers on-the-fly when users enter each digit.isValidNumber - full validation of a phone number for a region using length and prefix information.isPossibleNumber - quickly guessing whether a number is a possible phonenumber by using only the length information, much faster than a full validation.getExampleNumber/ getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.isNumberMatch - gets a confidence level on whether two numbers could be the same.getNumberType - gets the type of the number based on the number itself able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).Parsing/formatting/validating phone numbers for all countries/regions of the world.It also supports countries outside the US. Is a possible number but not a valid number. I know it is not regex but it does exactly what you want.įor example, it will recognize that: 15555555555 I would also suggest looking at the " libphonenumber" Google Library.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |