Validating phone number in uitextfield
I am using the following code to display phone number in text field in the following format
and the format is 123-456-7890
and the code is working fine
and the code is as follows
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if ([string isEqualToString:@""])
{
return YES;
}
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound|| [string rangeOfString:@"-"].location != NSNotFound|| charCount > 12) {
return NO;
}
if (charCount == 3 || charCount == 7) {
newString = [newString stringByAppendingString:@"-"];
}
amountField.text = newString;
return NO;
}
I am using the UItextfield delegate method.
But when I am editing the text field up to "-" that mean (if i tried to change the number in text field 123-456-7890 it is not validating that mean 123-456 if i again enter the remaining num开发者_开发问答ber from here it is displaying as 123-4567890
can any one please help me how to validate this thing
You have a good tutorial about validating both US and international phone numbers
http://blog.stevenlevithan.com/archives/validate-phone-number
This is how I have done it before in the past. This is in no way efficient, but gets the job done, and is for U.S. numbers only.
- (NSString *) getLongPhoneNumber
{
NSString *storedNumber = @"15125551212"
/* ^^ some incoming phone number */
if(storedNumber == nil){
storedNumber = @"5125551212";
}
//NSLocale *locale = [NSLocale currentLocale];
//NSString *localeString = [locale localeIdentifier];
NSString *tempStr = nil;
NSRange range;
range.length = 3;
range.location = 3;
NSString *areaCode = [storedNumber substringToIndex:3];
NSString *phone1 = [storedNumber substringWithRange:range];
NSString *phone2 = [storedNumber substringFromIndex:6];
tempStr = [NSString stringWithFormat:@"1 (%@) %@-%@", areaCode, phone1, phone2];
return tempStr;
}
this code use easy to check 111-111-1111 format phone numbers
NSString *phoneRegEx = @"[0-9]{3}+-[0-9]{3}+-[0-9]{4}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegEx];
if([phoneTest evaluateWithObject:phone]!=YES){
//your code
}
else
{
//your code
}
Have a look at this code this might help u a bit
txtlpmobile.text is the string(Mobile no ur gonna enter)
int length = [self getLength:txtLpMobile.text];
if(length == 10) {
if(range.length == 0)
return NO;
}
if(length == 3){
NSString *num = [self formatNumber:txtLpMobile.text];
txtLpMobile.text = [NSString stringWithFormat:@"(%@) ",num];
if(range.length > 0) {
txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
} else if(length == 6) {
NSString *num = [self formatNumber:txtLpMobile.text];
txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]];
if(range.length > 0) {
txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
}
}
NSUInteger newLength;
newLength = [txtLpMobile.text length] + [string length] - range.length;
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));
for formatting number
-(NSString*)formatNumber:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
if(length > 10)
{
mobileNumber = [mobileNumber substringFromIndex: length-10];
}
return mobileNumber;
}
for getting length
-(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 = [mobileNumber length];
return length;
}
精彩评论