Phone Number Validation

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

if (!string.length)
return YES;
if (textField == self.phone_TxtFeild)
{
if(range.location==0){

if ([string isEqualToString:@"9"] || [string isEqualToString:@"8"] || [string isEqualToString:@"7"]) {
return YES;

}
else{
return NO;
}

}
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];

if (numberOfMatches == 0)
return NO;
return !([newString length] > 10);
}

return YES;

}
Previous
Next Post »