iphone - Convert number to phone format using regular expression -
my problem have text field number , must convert number phone format 1 (xxx) xxx-xxxx. have tried regular expression code:
wholetext = [wholetext stringbyreplacingoccurrencesofstring:@"(\\d{1,3})(\\d{0,3})(\\d{0,4})" withstring:@"($1) $2-$3" options:nsregularexpressionsearch range:nsmakerange(0, wholetext.length)]; nslog(@"wholetext = %@", wholetext);
if gradually enter text in text field, nslog
output this:
wholetext = (1) - wholetext = (12) - wholetext = (123) - wholetext = (123) 4- wholetext = (123) 45- wholetext = (123) 456- wholetext = (123) 456-7
so problem do not need brackets , hyphens if there no number before it, i.e. closing bracket should appear after enter 4th number , hyphen should appear after enter 7th number.
use utility
uitextfield subclass allows number input in predefined format.
http://www.cocoacontrols.com/controls/reformattednumberfield
Comments
Post a Comment