判断是否为有效电话号码的方法

本文提供了一种使用正则表达式来验证中国移动、联通及电信手机号码的方法,并通过Objective-C代码实现来确保输入的电话号码符合中国大陆的电话号码标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /**

         * 移动号段正则表达式

         */

        NSString *CM_NUM =@"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";

        /**

         * 联通号段正则表达式

         */

        NSString *CU_NUM =@"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";

        /**

         * 电信号段正则表达式

         */

        NSString *CT_NUM =@"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";

        NSPredicate *pred1 = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", CM_NUM];

        BOOL isMatch1 = [pred1evaluateWithObject:mobile];

        NSPredicate *pred2 = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", CU_NUM];

        BOOL isMatch2 = [pred2evaluateWithObject:mobile];

        NSPredicate *pred3 = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", CT_NUM];

        BOOL isMatch3 = [pred3evaluateWithObject:mobile];

        

        if (isMatch1 || isMatch2 || isMatch3) {

            returnnil;

        } else {

            return@"请输入正确的电话号码";

        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值