python判断给定的手机号是否存在

    今天在注册一个学习网站的时候手机号输错了,系统直接就提醒请输出正确的手机号,于是就想用python做一个手机号合法性识别的程序,实践起来也很简单,就不多解释了,如下:

#!usr/bin/env python
#encoding:utf-8


'''
__Author__:沂水寒城
功能:判断手机号是否存在
中国联通手机号频段:
                   130,131,132,155,156,185,186,145,176
中国移动手机号频段:
                   134, 135 , 136, 137, 138, 139, 147, 150, 151,
                   152, 157, 158, 159, 178, 182, 183, 184, 187, 188
中国电信手机号频段:
                   133,153,189
'''
 
import re
import sys
import os
 
def judgePhoneNumberRight(phoneNum):
    '''
    判断指定的手机号是否存在
    '''
    if len(str(phoneNum))!=11:
        flag=False
    else:
        if not str(phoneNum).isdigit():
            flag=False
        else: 
            phone_rule=re.compile('^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}')
            res_list=re.findall(phone_rule,str(phoneNum))
            if res_list:
                flag=True
            else:
                flag=False
    if flag:
        print '{} is right!'.format(phoneNum)
    else:
        print '{} is wrong!'.format(phoneNum)


if __name__=='__main__':
    phoneNum='13465787'
    judgePhoneNumberRight(phoneNum)
    
    phoneNum='145203922X3'
    judgePhoneNumberRight(phoneNum)

    phoneNum='14520392233'
    judgePhoneNumberRight(phoneNum)

    phoneNum='15762351234'
    judgePhoneNumberRight(phoneNum)

      结果如下:

13465787 is wrong!
145203922X3 is wrong!
14520392233 is wrong!
15762351234 is right!

猜你喜欢

转载自blog.csdn.net/Together_CZ/article/details/83715184