Android Question Get valid phone number fron contacts

Pooya1

Active Member
Licensed User
Hi
I can read contacts list and save it to database
But i need save their without country code or any extra number in phone
Example user maybe have below phone numbers
1- +9725000000
2- 9725000000
3- 5000000

In first number i can remove 972 and save phone numbers
But in second and third number,i dont know what is country code
Or how detect what is country code
In my app,there are all countries that i need save each user phones numbers according to phone number in database

Regards
 

Pooya1

Active Member
Licensed User
Finally i found solution
I user's contacts :
If user add contact with phone number +9725000000,so android detect format and show it "+972 500 000000"
So we can remove +972 in first
If use enter invalid country code,it is user mistake not you
If user number 9725000000,android show it only "9725000000" and it is user mistake
If enter 05000000 you can remove zero from first
I attach code
B4X:
Sub ParsePhoneNumber(Number As String) As String
    
    Dim result As String
    
    If Number.StartsWith("+") Then
        
        Dim res() As String
        res    =    Regex.Split(" ",Number)
        
        If res.Length = 0 Then
            Return Number
        End If
        
        If res.Length = 1 Then
            Return Number
        End If
        
        Dim temp As String
        For i = 1 To res.Length - 1
            temp = temp & res(i)
        Next
        
        result    =    temp
    
    Else if Number.StartsWith("0") Then
        result    =    Number.SubString(1)
        result    =    result.Replace(" ","")
    
    Else
        result    =    Number
    End If
    
    Return result
    
End Sub
 
Upvote 0
Top