python檢測手機號碼是否合法


 1 !/usr/bin/python
 2 #encoding:utf-8
 3 #這是一個用來檢測用戶輸入手機號碼是否合法的小腳本。
 4 
 5 def phonecheck(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                 if  s.isdigit():
14                         #檢測前綴是否是正確。
15                         if s[:3] in phoneprefix:
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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM