iPhone简单的电话号码验证 [英] IPhone simple phone number validation

查看:198
本文介绍了iPhone简单的电话号码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iphone的stackoverflow上找到了所有类型的电话号码验证,但似乎没有一个验证它们似乎只是改变了格式。示例电话号码格式

I find all kinds of phone number validations on stackoverflow for the iphone but none seem to validate they just seem to change the format. example Phone number formatting.

目标:通过提醒输入此号码。北美人有或没有破折号。

Goals: this number is being entered via an alert. North American with or with out dashes.

所以有一些代码但是每件事都是无效的想法。

so have some code but every thing is coming up as invalid any ideas.

- (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);
   }
}


推荐答案

首先,真正的电话号码验证非常复杂。您是在限制自己使用NANP(北美编号方案),还是在寻找E.164号码?看起来你试图主要匹配E.164,虽然这通常不是大多数人会输入的数字。

First, real phone number validation is pretty complicated. Are you limiting yourself to NANP (North America Numbering Plan), or are you looking for E.164 numbers? It looks like you're trying to mostly match E.164, though this isn't usually the kind of number that most people would enter.

你的比赛需要一个加号,接着是七到十五长的数字序列,它们之间有可选的空格。你是这个意思吗?你传递了什么字符串?

Your match requires a plus, followed by a sequence of digits between seven and fifteen long with optional spaces between them. Is this what you mean? What strings are you passing in?

请注意,从iOS4开始, NSRegularExpression 对于这比 NSPredicate

Note that as of iOS4, there is NSRegularExpression that is a bit better for this than NSPredicate.

这篇关于iPhone简单的电话号码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆