天天看點

python判斷手機号碼是否正确_python檢測手機号碼是否合法

1 !/usr/bin/python2 #encoding:utf-8

3 #這是一個用來檢測使用者輸入手機号碼是否合法的小腳本。

4

5 defphonecheck(s):6 #号碼字首,如果營運商啟用新的号段,隻需要在此清單将新的号段加上即可。

7 phoneprefix=['130','131','132','133','134','135','136','137','138','139','150','151','152','153','156','158','159','170','183','182','185','186','188','189']8 #檢測号碼是否長度是否合法。

9 if len(s)<>11:10 print "The length of phonenum is 11."

11 else:12 #檢測輸入的号碼是否全部是數字。

13 ifs.isdigit():14 #檢測字首是否是正确。

15 if s[:3] inphoneprefix:16 print "The phone num is valid."

17 else:18 print "The phone num is invalid."

19 else:20 print "The phone num is made up of digits."

21

22

23 if __name__=="__main__":24 phonenum=raw_input("Input your phone num:")25 phonecheck(phonenum)