Checking a Simple IPhone Phone Number

I find all kinds of phone number checking in stackoverflow for iphone, but no one seems to check that they just change the format. Example Formatting a phone number .

Purpose: This number is entered through a warning. North American with or with a dash.

so you have the code, but every thing appears as invalid any idea.

- (void)textFieldDidEndEditing:(UITextField *)textField { NSString * str=@ "^\\+(?:[0-9] ?){6,14}[0-9]$"; NSPredicate *no=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str]; if([no evaluateWithObject:textField.text]==NO) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Enter correct contact no." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; } else{ NSString *textValue = textField.text; NSLog(@"Value: %@", textValue); } } 
+6
source share
3 answers

Firstly, real checking a phone number is quite complicated. Are you limiting yourself to NANP (North American Numbering Plan) or are you looking for E.164 numbers? It sounds like you're trying to basically match E.164, although this is usually not the number that most people will enter.

Your match requires a plus, and then a sequence of numbers from seven to fifteen long with additional spaces between them. Is that what you mean? What lines do you go through?

Note that with iOS4 there is NSRegularExpression , which is slightly better for this than NSPredicate .

+3
source

EDIT . I read the question again and saw that you want to confirm, not select the phone number, so I moved the section to NSDataDetector .

You can use NSDataDetector (which is a specialized subclass of NSRegularExpression ) to search for phone numbers in NSString (in my opinion, this uses the default iPhone default phone number algorithm).

 NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error]; NSUInteger numberOfMatches = [detector numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])]; 

If you use a UITextView (but probably not a UITextField , as you are using here), you can allocate a phone number using the iPhone default phone number algorithm by setting its dataDetectorTypes property:

 textView.dataDetectorTypes = UIDataDetectorTypePhoneNumber; 

See UIDataDetectorTypes for other types of data detectors.

+26
source

Using this check, you can enter only 10 numbers. You can change its length according to your requirement.

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { BOOL valid = [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", REGEX_FOR_INTEGERS] evaluateWithObject:_textFieldValidate.text]; if(valid){ if(range.length + range.location > textField.text.length) { return NO; } NSUInteger newLength = [textField.text length] + [string length] - range.length; return newLength <= 10; } return YES; } 

Hope this helps you.

0
source

Source: https://habr.com/ru/post/895794/


All Articles