2012-01-25 226 views
2

我想有僅13的數字值或13numeric值可以用「+」爲前綴的sysmbol.so +不是強制性 示例:1234567891234 另一個例子是:1234567891234驗證電話號碼

Telephone number format should be international,Is there any Regex for phone number validation in iPhone

我已經嘗試了上面的鏈接,但是這個+1234545但我想只有13個numarals或+可以用13個數字作爲前綴。

請讓我知道,我可以在這裏

改變它,這個是我試圖

NSString * forNumeric = @"^\\+(?:[0-9] ?){6,14}[0-9]$"; 

    BOOL isMatch = [[textFieldRounded text] isMatchedByRegex:forNumeric]; 

    if (isMatch == YES){ 

     NSLog(@"Matched"); 
    } 
    else { 
     NSLog(@"Not matched"); 
    } 
+0

不可能使用正則表達式嗎?請讓我知道 – user198725878

回答

1

^(\+?)(\d{13})$應該做的伎倆,逃避客觀C使用斜線。 13位數字,帶有選項+前綴。

如果你想玩正則表達式,你可以使用像this one這樣的服務進行視覺反饋,非常方便。

NSString * forNumeric = @"^(\\+?)(\\d{13})$"; 
+1

在這種情況下,用戶必須輸入一個13位數字。如果我們希望用戶輸入小於13的數字,可以做些什麼?也許9-10位數字。 – ScarletWitch

+3

'NSString * forNumeric = @「^(\\ +?)(\\ d {a,b})$」;「 只需將a和b替換爲您想要的包含範圍,例如a = 9,b = 10。 –

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

如果使用UITextField則:

textField.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
+0

'dataDetectorTypes'不適用於文本字段。 – Nishant

0

以下是我用於驗證英國手機號碼做:

- (BOOL) isValidPhoneNumber 
{ 
    NSString * regex = @"((07|00447|004407|\\+4407|\\+447)\\d{9})"; 
    NSPredicate *testPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 
    BOOL validationResult = [testPredicate evaluateWithObject: self]; 
    return validationResult; 
} 

看它是否有助於你

1

這個怎麼樣?

NSString *forNumeric = @"\\+?[0-9]{6,13}"; 
NSPredicate *predicate; 
predicate = [NSPredicate predicateWithFormat:@"self matches %@", forNumeric]; 
BOOL isMatch = [predicate evaluateWithObject:@"+1234567890123"]; 

if (isMatch) NSLog(@"Matched"); 
else NSLog(@"Not matched"); 
4
NSString * regex = @"((07|00447|004407|\\+4407|\\+447)\\d{9})"; 

已經找到了0領先或龍頭+44一次,爲什麼再次搜索呢?

基本簡化導致

NSString * regex = @"((07|00440?7|\\+440?7)\\d{9})"; 

然後

NSString * regex = @"((07|(00|\\+)440?7)\\d{9})"; 

然後

NSString * regex = @"((0|(00|\\+)440?)7\\d{9})"; 

但00不是唯一的共同撥號前綴,011在美國使用和加拿大。

並稱,和輪轉動的順序,得到:

NSString * regex = @"(^((0(0|11)|\\+)440?|0)7\\d{9}$)"; 

或優選

NSString * regex = @"(^(?:(?:0(?:0|11)|\\+)(44)0?|0)(7\\d{9}$))"; 

允許00447,011447,447,004407,0114407,4407,07在開始,並與非捕獲組。

對於較寬的輸入格式匹配,允許不同的標點符號(連字符,括號,空格)使用

NSString * regex = @"(^\\(?(?:(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)(44)\\)?[\\s-]?\\(?(?:0\\)?[\\s-]?\\(?)?|0)(7\\d{9})$)"; 

提取$ 1的44個國家或地區代碼(如果爲空號輸入爲07 ...)和10數字爲2美元的NSN。

但是,請注意,從070和076(除07624之外)開始的數字不是手機號碼。

最終圖案:

NSString * regex = @"(^\\(?(?:(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)(44)\\)?[\\s-]?\\(?(?:0\\)?[\\s-]?\\(?)?|0)(7([1-5789]\\d{2}|624)\\)?[\\s-]?\\d{6}))$)"; 

提取NSN在$ 2,則從中除去所有的非數字用於進一步處理。