简体   繁体   中英

Javascript/Typescript: How do I detect valid contacts and phone numbers in Android app

In my Android app, I would like to display list of contacts and indicate if they are in the network of my app or not. However, I do not want to include all the unwanted contacts (ie ex: toll-free numbers, numbers for checking data balance etc..) I would like to display only valid 'contacts'. If possible, I would like to display only smart phone users. How do I go about this ?

I am currently filtering out these unwanted contacts using few rules like: 1) If a number starts with 1-800.. 2) If the formatted number is not 10-digits

Q1. Is there a simpler standard way to do this ? Q2. Is there a good set of rules for implementing this ?

You are going to want to use a Regular Expression .

If you are new to RegEx I'd recommend reading up on them as they are vital for most web apps.

If you are pushed for time there is a huge library of expressions here .

Although I highly recommend learning how to write RegEx.

Some examples:


/[0-9]+

Will match any character in the range 0-9 , there must be at least 1 character.

23823 - matched
9382 - matched
2 - matched
foo - not matched


/\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b

This is a very simple North American Phone Number matcher

0123456789 - matched
555.123.4567 - matched

As you can see there is a lot of room for complexity with RegEx - this is a very basic RegEx and it does not account for all possible edge cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM