Check if exists phone number in String

Viewed 4190

Hey I would like to check if UITextView (maybe this could be relevant or not) contains phone number in text. I am using Swift 2.3, but if you got it in Swift 3 I will try to translate it.

Should work for these inputs for exemple:

  • "Good morning, 627137152"
  • "Good morning, +34627217154"
  • "Good morning, 627 11 71 54"

The last case it's easy if other works, just replace " " occurences to "" and check the previous function containsPhone.

I have tried this solution but doesn't works with the first case: (Find phone number in string - regular)

Thank you.

4 Answers

Swift 5 Very Easy way with two line of Code this will help you only to get a decimal number from string

//MARK:- if the Single Phone number in String 
var tempphone = "+34627217154 \n Good morning"
tempphone = tempphone.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")

print(tempohone) 
//MARK:- Result No: 34627217154


//MARK:- if the Multiple Phone number in String 
var tempphone = "Good morning, 627137152 \n Good morning, +34627217154 \n Good morning, 627 11 71 54"
tempphone = tempphone.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")

print(tempohone) 
//MARK:- Result No:  62713715234627217154627117154
Related