Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the valid phone number

Tags:

ios

swift

I have one text field for enter the phone number and user have to press OK button.

Then I write some function to check whether entered number is valid number or 10 digit number. And I don't want to add country code. That I have separately.

But when I press OK button its give me uialert - wrong number for all number including my own number. I don't know any code I missed?

    func validate(value: String) -> Bool {
        let PHONE_REGEX = "^\\d{3}-\\d{3}-\\d{4}$"
        var phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
        var result =  phoneTest.evaluateWithObject(value)
        return result
    }

    @IBAction func confirmAction(sender: AnyObject) {

        if validate(phoneNumber.text!)
        {
            print("Validate EmailID")
            let phone = countryCode.text! + phoneNumber.text!
            UserNetworkInterface().generateSms(phone, onCompletion: nil)
            performSegueWithIdentifier("ConfirmSmsCode", sender: self)
        }
        else
        {
            print("invalide EmailID")
            let alert = UIAlertView()
            alert.title = "Message"
            alert.message = "Enter Valid Contact Number"
            alert.addButtonWithTitle("Ok")
            alert.delegate = self
            alert.show()
        }
  }

Updated :

 @IBAction func confirmAction(sender: AnyObject) {

        if let phoneNumberValidator = phoneNumber.isPhoneNumber
        {
            print("Validate EmailID")
            let phone = countryCode.text! + phoneNumber.text!
            UserNetworkInterface().generateSms(phone, onCompletion: nil)
            performSegueWithIdentifier("ConfirmSmsCode", sender: self)


        }
        else
        {
            print("invalide EmailID")
            let alert = UIAlertView()
            alert.title = "Message"
            alert.message = "Enter Valid Contact Number"
            alert.addButtonWithTitle("Ok")
            alert.delegate = self
            alert.show()
            phoneNumber.text = ""


        }
                    // Number valid


  }
like image 482
user5513630 Avatar asked Apr 22 '16 09:04

user5513630


People also ask

How can I check if a mobile number is valid?

Mobile Number validation criteria:The first digit should contain numbers between 6 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.

How can I check a phone number?

Whitepages.com is one of the most accurate options for landlines, but not all the information is free. Go to www.whitepages.com to see your options for a reverse address search or reverse lookup on a phone number. Addresses.com and Anywho.com are two other sites that do free reverse phone number searches for landlines.

Is there a free way to verify a phone number?

AnyWho is an online service that lets you reverse lookup and identify phone numbers for free. Just enter the phone number you're looking for and it'll display all the details about it.

How do I identify a cell phone number?

ZLOOKUP and USPhonebook are free options that give results for people and businesses. Go ahead, turn to Google. Type in the person's name and put it in quotes for more accurate results. You can also try adding the area code or a city, or words like “contact,” “number” or “cell.”


2 Answers

Try this.

Make an extension to String.

Swift 4

extension String {
    var isPhoneNumber: Bool {
        do {
            let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
            let matches = detector.matches(in: self, options: [], range: NSRange(location: 0, length: self.count))
            if let res = matches.first {
                return res.resultType == .phoneNumber && res.range.location == 0 && res.range.length == self.count
            } else {
                return false
            }
        } catch {
            return false
        }
    }
}

Older Swift Versions

extension String {
    var isPhoneNumber: Bool {
        do {
            let detector = try NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue)
            let matches = detector.matchesInString(self, options: [], range: NSMakeRange(0, self.characters.count))
            if let res = matches.first {
                return res.resultType == .PhoneNumber && res.range.location == 0 && res.range.length == self.characters.count
            } else {
                return false
            }
        } catch {
            return false
        }
    }
}

Usage:

override func viewWillAppear(animated: Bool) {

//Sample check
let phoneString = "8888888888"

let phoneNumberValidator = phoneString.isPhoneNumber
print(phoneNumberValidator)

}
like image 55
Alvin George Avatar answered Oct 22 '22 10:10

Alvin George


Swift 3

For those of you who would like the phone number to have a minimum of 10 characters use the below code (Amended @Alvin George code)

extension String {
    var isPhoneNumber: Bool {
        do {
            let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
            let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.characters.count))
            if let res = matches.first {
                return res.resultType == .phoneNumber && res.range.location == 0 && res.range.length == self.characters.count && self.characters.count == 10
            } else {
                return false
            }
        } catch {
            return false
        }
    }
} 

Usage

ooverride func viewDidLoad() {
    super.viewDidLoad()

    //Example
    let phoneNumberString = "8500969696"

    let phoneNumberValidation = phoneNumberString.isPhoneNumber
    print(phoneNumberValidation) 
    // Prints: true

}
like image 34
SpaceX Avatar answered Oct 22 '22 12:10

SpaceX