Phone number format and validation library [closed]
Google recently released libphonenumber for parsing, formatting, storing and validating international phone numbers.
Google recently released libphonenumber for parsing, formatting, storing and validating international phone numbers.
ITU-T recommendation E.164 says you need 3 digits for the country code and up to 15 digits for the directory number within the country dialing plan. And, many people add some punctuation. For example: +1.212.555.1212 is a North American number. It could also be rendered (212) 555-1212 in a North American centric app. 32 characters … Read more
ContentResolver cr = mContext.getContentResolver(); //Activity/Application android.content.Context Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if(cursor.moveToFirst()) { ArrayList<String> alContacts = new ArrayList<String>(); do { String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +” = ?”,new String[]{ id }, null); while (pCur.moveToNext()) { String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); alContacts.add(contactNumber); break; } pCur.close(); } } … Read more
Definitely regex: string CleanPhone(string phone) { Regex digitsOnly = new Regex(@”[^\d]”); return digitsOnly.Replace(phone, “”); } or within a class to avoid re-creating the regex all the time: private static Regex digitsOnly = new Regex(@”[^\d]”); public static string CleanPhone(string phone) { return digitsOnly.Replace(phone, “”); } Depending on your real-world inputs, you may want some additional logic … Read more
Use isGlobalPhoneNumber() method of PhoneNumberUtils to detect whether a number is valid phone number or not. Example System.out.println(“….g1…”+PhoneNumberUtils.isGlobalPhoneNumber(“+912012185234”)); System.out.println(“….g2…”+PhoneNumberUtils.isGlobalPhoneNumber(“120121852f4”)); The result of first print statement is true while the result of second is false because the second phone number contains f.
Strings & VARCHAR. Do not try storing phone numbers as actual numbers. it will ruin the formatting, remove preceding 0s and other undesirable things. You may, if you choose to, restrict user inputs to just numeric values but even in that case, keep your backing persisted data as characters/strings and not numbers. Be aware of … Read more
Try this: preg_replace(‘/[^0-9]/’, ”, ‘604-619-5135’); preg_replace uses PCREs which generally start and end with a /.
This answer assumes you want the following format: (000) 000-0000 (as the OP states). There are multiple different ways to implement this, but here are a couple different approaches: If you want to simply mask the number on the blur event (when the field loses focus), then you could use the following: document.getElementById(‘phone’).addEventListener(‘blur’, function (e) … Read more
Although this has already been answered, but here is the complete function to get the contact name from number. Hope it will help others: public static String getContactName(Context context, String phoneNumber) { ContentResolver cr = context.getContentResolver(); Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null); if (cursor == null) { … Read more
It seems to me that you’re putting more effort into this than it warrants. Consider: If your purpose is to guard against mis-entered phone numbers, then you can probably catch well over 90% of them with just a very simple check. If your purpose is to try to force users to provide a valid number … Read more