1. 程式人生 > >2018年最新電話號碼正則表示式校驗方法

2018年最新電話號碼正則表示式校驗方法

正則表示式

^1(3[0-9]|4[57]|5[^4]|6[6]|7[0-8]|8[0-9]|9[8-9])\\d{8}$

iOS使用方法

+ (BOOL)checkPhoneNumber:(NSString *)phoneNumber{
    /*
     ** 電訊號段:133/153/180/181/189/177
     ** 聯通號段:130/131/132/155/156/185/186/145/176
     ** 移動號段: 134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
     ** 虛擬運營商:170
     */
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[^4]|6[6]|7[0-8]|8[0-9]|9[8-9])\\d{8}$"; NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE]; return [regextestmobile evaluateWithObject:phoneNumber]; }

Android使用方法:

public static boolean isMobileNO(String mobileNums) {
    /*
     ** 電訊號段:133/153/180/181/189/177
     ** 聯通號段:130/131/132/155/156/185/186/145/176
     ** 移動號段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/149/178
     ** 虛擬運營商:170
     */
String telRegex = "^((13[0-9])|(14[5,7])|(15[^4,\\D])|(166)|(17[0-8])|(18[^4,\\D])|(199)|(198))\\d{8}$"; if (TextUtils.isEmpty(mobileNums)) return false; else return mobileNums.matches(telRegex); }

java使用方法:

public static boolean isMobileNO(String mobiles) {  
    boolean
flag = false; try { Pattern p = Pattern .compile("^((13[0-9])|(14[5,7])|(15[^4,\\D])|(166)|(17[0-8])|(18[^4,\\D])|(199)|(198))\\d{8}$"); Matcher m = p.matcher(mobiles); flag = m.matches(); } catch (Exception e) { flag = false; } return flag; }