Validation for textfield is not working

Posted by MaheshBabu on Stack Overflow See other posts from Stack Overflow or by MaheshBabu
Published on 2011-01-11T05:35:18Z Indexed on 2011/01/11 5:53 UTC
Read the original article Hit count: 190

Filed under:

Hi folks,

I am using 4 Textfields in my application.

I need to validate those textfields like,These 4 textfields Allow only o,1,....9 and .

For that i use the code like this.

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

 static NSCharacterSet *charSet = nil;
 if(!charSet) {
  charSet = [[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet] retain];
 }
 NSRange location = [string rangeOfCharacterFromSet:charSet];
 return (location.location == NSNotFound);

And first textfield allows digits 10,second third and fourth textfields allows less than 100.

For that my code is

  if (textField.tag == 1) {

      NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
      double homevaluedouble = [newString doubleValue];
      return !(homevaluedouble > 10000000000);


      } 
if (textField.tag == 2) {

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

  return !(validate > 100);  
  }
if (textField.tag == 3) {

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

  return !(validate > 100);  
  }

I write this code in

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

But textfields allows 0,1,..9 and . but digit limit is not working,

if i remove 0,1,...9 then limit to textfield working.

i am getting confuse with it.

can any one pls help me,How can i resolve it.

Thank u in advance.

© Stack Overflow or respective owner

Related posts about iphone