Asdfg
Asdfg

Reputation: 12203

PhoneGap - only show contacts with phone numbers

Is there a way to only show contacts that has phone number?

Right now i am getting all the contacts and then looping thru each and finding out their phone number but i was wondering if there is a way to pass a parameter to contactFindOptions object so that it only picks up contacts that has phone number?

This is how my code currently looks like:

var options = new ContactFindOptions();
options.filter=""; //Can i pass something here to pick only contacts with phone number
options.multiple=true; 
var fields = ["displayName", "phoneNumbers"];
navigator.contacts.find(fields, onSuccess, onError, options);


function onSuccess(contacts) {
    for (var i=0; i<contacts.length; i++) {
        console.log("Display Name = " + contacts[i].displayName);
        if(null != contacts[i].phoneNumbers)
            {
                for(var j=0;j<contacts[i].phoneNumbers.length;j++)
                {
                      console.log("Name = " + contacts[i].displayName);
                      console.log("Phone = " + contacts[i].phoneNumber[j].value);

                }
            }
    }
}

Upvotes: 9

Views: 4518

Answers (3)

Niru
Niru

Reputation: 489

yes, we can use hasPhoneNumber filter option. Code snippet is as follows:

            var contactFindOptions = new ContactFindOptions();
            contactFindOptions.filter = "";
            contactFindOptions.multiple = true;
            contactFindOptions.hasPhoneNumber = true;
            navigator.contacts.find(
                    ["phoneNumbers"],
                    function (contacts) {
                       // you will get contacts in this callback success function
                    },
                    function (e) {
                        if (e.code === ContactError.NOT_SUPPORTED_ERROR) {
                           console.log("Searching for contacts is not supported.");
                        } else {
                            console.log("Search failed: error " + e.code);
                        }
                    },
                    contactFindOptions);

Note: hasPhoneNumber(Android only): Filters the search to only return contacts with a phone number informed. (Boolean) (Default: false)

Upvotes: 1

Anthony
Anthony

Reputation: 462

This plugin looks like the best approach: https://github.com/dbaq/cordova-plugin-contacts-phone-numbers.

It only searches for contacts with phone numbers.

Upvotes: 1

user1650613
user1650613

Reputation: 147

I believe Cordova API does not allow filtering to accomplish what you require.

From their documentation here: http://docs.phonegap.com/en/2.5.0/cordova_contacts_contacts.md.html#contacts.find

It says:

The contactFindOptions.filter string can be used as a search filter when querying the contacts database. If provided, a case-insensitive, partial value match is applied to each field specified in the contactFields parameter. If a match is found in a comparison with any of the specified fields, the contact is returned.

I don't believe you can use this to determine a non null for the phone number field.

Upvotes: 0

Related Questions