how to validate textfield that allows only single decimal point in textfield?

ios, iphone, uitextfield, validation

Solution

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

NSString *sepStr;

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count]>=2)
   {
        sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
        NSLog(@"sepStr:%@",sepStr);
        if([sepStr length] >2)
       {
        return NO;
       }
    else 
       {
        return YES;
       }

   }
   return YES;
 }

Problem

I need to restrict user to enter only two digit after decimal point. I have achieved this by following code in textfield delegate shouldChangeCharactersInRange. But its allowing to enter more than one dot. how to restrict this? Thanks in advance. ``` NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSArray *sep = [newString componentsSeparatedByString:@"."]; if([sep count]>=2) { NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]]; NSLog(@"sepStr:%@",sepStr); return !([sepStr length]>2); } return YES; ```

Original source