- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == txtSignUpMobileNo)
{
int length = [self getLength:textField.text];
//NSLog(@"Length = %d ",length);
if(length == 10)
{
if(range.length == 0)
return NO;
}
if(length == 4)
{
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:@"%@ ",num];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:4]];
}
else if(length == 7)
{
NSString *num = [self formatNumber:textField.text];
//NSLog(@"%@",[num substringToIndex:4]);
//NSLog(@"%@",[num substringFromIndex:4]);
textField.text = [NSString stringWithFormat:@"%@ %@ ",[num substringToIndex:4],[num substringFromIndex:4]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"%@ %@",[num substringToIndex:4],[num substringFromIndex:4]];
}
return YES;
}
else
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 50) ? NO : YES;
}
return YES;
}
-(NSString*)formatNumber:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
//NSLog(@"%@", mobileNumber);
int length = (int)[mobileNumber length];
if(length > 10)
{
mobileNumber = [mobileNumber substringFromIndex: length-10];
//NSLog(@"%@", mobileNumber);
}
return mobileNumber;
}
-(int)getLength:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = (int)[mobileNumber length];
return length;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self resignKeyboard:0];
[textField resignFirstResponder];
return YES;
}
- (BOOL)validateEmail:(NSString *)email
{
//DLog(@"checkEmailString = %@",email);
BOOL emailFlg = NO;
NSArray *atArr = [email componentsSeparatedByString:@"@"];
//check with one @
if ([atArr count] == 2)
{
NSArray *dotArr = [atArr[1] componentsSeparatedByString:@"."];
//check with at least one .
if ([dotArr count] >= 2)
{
emailFlg = YES;
//all section can't be
for (int i = 0; i<[dotArr count]; i++)
{
if ([dotArr[i] length] == 0 || [dotArr[i] rangeOfString:@" "].location != NSNotFound)
{
emailFlg = NO;
}
}
}
}
return emailFlg;
}
No comments:
Post a Comment