iphone - problem while validation text field after -
i need validate textfield input allows 2 digits after .
. eg: if enter 2.333 wont allow if can able allow 2.33 only.
for use code
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { nsarray *sep = [textfield.text componentsseparatedbystring:@"."]; if([sep count]==2) { nsstring *sepstr=[nsstring stringwithformat:@"%@",[sep objectatindex:1]]; return !([sepstr length]=2); } }
if use return !([sepstr length]=2)
works fine wont allow enter next value.
but if remove value text field wont allow modifications. mean there no user interaction. can't able edit. can 1 please me. how can correct this. thank u in advance.
the problem code thomas clayson user still can't edit text field anymore when types example: "1234" , after inserts "." between 1 , 2, or if pastes number in textfield.
here's better solution:
- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string { nsstring *newstring = [textfield.text stringbyreplacingcharactersinrange:range withstring:string]; nsarray *sep = [newstring componentsseparatedbystring:@"."]; if([sep count]>=2) { nsstring *sepstr=[nsstring stringwithformat:@"%@",[sep objectatindex:1]]; return !([sepstr length]>2); } return yes; }
Comments
Post a Comment