-3

I'm new to iOS development. I want to know about how we can validate text field if it contains number with some specific range, say 10 for contact number ? Is there any predefined function available ?

Thank you.

iAkshay
  • 1,143
  • 1
  • 13
  • 35
  • 1
    possible duplicate http://stackoverflow.com/questions/21669736/validation-for-phone-number-in-ios – Sujay May 14 '15 at 08:04

2 Answers2

1
+ (BOOL) validatePassword:(NSString *)passWord
{
   NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
   NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
   return [passWordPredicate evaluateWithObject:passWord];
}
menextu
  • 77
  • 3
  • Thank you @menextu. I would like to know how this regex working here to check its in range of 10 characters ? Should I use NSString length/count to do so ? – iAkshay May 15 '15 at 15:16
0

To have textfield to accept only numbers and only till 10 digit length you can use this function.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
   NSString *acceptedCharsters = @"0123456789";

 NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 10) ? NO : YES;

NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:acceptedCharsters] invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    return [string isEqualToString:filtered];


return YES;
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Shruti
  • 1,849
  • 1
  • 13
  • 21