讲正题之前我们先来看一个例子:https://reg.jd.com/reg/person?ReturnUrl=https%3A//www.jd.com/
这是京东的注册页面,打开页面我们就看到这些要求输入个人信息的提示。
假如我们随意的在手机号码这一栏输入一个11111111111,它会提示我们格式有误。
这个功能是怎么实现的呢?
假如现在你用python写一段代码,类似:
phone_number = input('please input your phone number : ')
你怎么判断这个phone_number是合法的呢?
根据手机号码一共11位并且是只以13、14、15、18开头的数字这些特点,我们用python写了如下代码:
1 while True: 2 phone_number = input('please input your phone number : ') 3 if len(phone_number) == 11 \ 4 and phone_number.isdigit()\ 5 and (phone_number.startswith('13') \ 6 or phone_number.startswith('14') \ 7 or phone_number.startswith('15') \ 8 or phone_number.startswith('18')): 9 print('是合法的手机号码') 10 else: 11 print('不是合法的手机号码') 12 13 判断手机号码是否合法1