ios - how to eliminate the occurence of a certain character from a textfield and have it present only at the prefix -
i have uitextfield lets user type in phone number want test uitextfield such user shouldnt enter "+" anywhere else in textfield apart prefix.
is there way can test condition? have done far,please note fourth textfield using tag distinguish other textfields
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { if (textfield.tag==3) { static nscharacterset *charset = nil; if(!charset) { charset = [[nscharacterset charactersetwithcharactersinstring:@"0123456789+"] invertedset]; } nsrange location = [string rangeofcharacterfromset:charset]; return (location.location == nsnotfound); // delete characters backspace if ([string isequaltostring:@""]) { return yes; } } return yes; } the above condition allows numbers , "+" symbol thanks
try wont take + anywhere else except first position ,
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { if (textfield.tag==3) { if([textfield.text length]>1){ if([string isequaltostring:@"+"]) { return no; } else { return yes; } } return yes; } }
Comments
Post a Comment