ios - Follow up to phone formatting: More than 1 phone number field. -


i have question regarding phone number formatting code in swift 3 / xcode. vikzilla supplied excellent code formatting. however, have 2 phone fields in app. 1 phone number, other fax number. mentioned, answer supplied vikzilla perfect can't life of me figure out need edit in code second field (fax number) without either throwing " invalid redeclaration of 'textfield..." error. have renamed 2nd function etc. no errors thrown formatting of fax field doesn't work... help? below code. mentioned works perfect not sure how edit work on 2nd. field (faxnumber). thanks!

func textfield(_ textfield: uitextfield, shouldchangecharactersin range: nsrange, replacementstring string: string) -> bool {     if (textfield == self.phonefield){         let newstring = (textfield.text! nsstring).replacingcharacters(in: range, with: string)         let components = newstring.components(separatedby: nscharacterset.decimaldigits.inverted)          let decimalstring = components.joined(separator: "") nsstring         let length = decimalstring.length         let hasleadingone = length > 0 && decimalstring.character(at: 0) == (1 unichar)          if length == 0 || (length > 10 && !hasleadingone) || length > 11 {             let newlength = (textfield.text! nsstring).length + (string nsstring).length - range.length int              return (newlength > 10) ? false : true         }         var index = 0 int         let formattedstring = nsmutablestring()          if hasleadingone {             formattedstring.append("1 ")             index += 1         }         if (length - index) > 3 {             let areacode = decimalstring.substring(with: nsmakerange(index, 3))             formattedstring.appendformat("(%@)", areacode)             index += 3         }         if length - index > 3 {             let prefix = decimalstring.substring(with: nsmakerange(index, 3))             formattedstring.appendformat("%@-", prefix)             index += 3         }          let remainder = decimalstring.substring(from: index)         formattedstring.append(remainder)         textfield.text = formattedstring string         return false     } else {         return true     } } 

you need 1 shouldchangecharactersin method. remove initial if (and else). code work both text fields.

if have more 2 phone number text fields update if be:

if (textfield == self.phonefield || textfield == self.faxfield) { 

as side note, code work phone numbers in usa , canada, not other countries.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -