Python验证手机号码 [英] Python validation mobile number
本文介绍了Python验证手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试验证一个手机号码,下面是我迄今为止所做的,但似乎没有起作用。
I'm trying to validate a mobile number, below is what I have done so far but it does not appear to work.
我需要它来升级当通过的值看起来不像移动号码时的验证错误。手机号码可以是10到14位数字,从0或7开始,并且可以添加44或+44。
I need it to rise a validation error when the value passed does not look like a mobile number. Mobile numbers can be 10 to 14 digits long start with 0 or 7 and could have 44 or +44 added to them.
def validate_mobile(value):
""" Raise a ValidationError if the value looks like a mobile telephone number.
"""
rule = re.compile(r'/^[0-9]{10,14}$/')
if not rule.search(value):
msg = u"Invalid mobile number."
raise ValidationError(msg)
推荐答案
正则表达式符合您的描述
The following regex matches your description
r'^(?:\+?44)?[07]\d{9,13}$'
这篇关于Python验证手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文