天天看點

python判斷手機号碼是否正确,Python驗證手機号碼

python判斷手機号碼是否正确,Python驗證手機号碼

I'm trying to validate a mobile number, below is what I have done so far but it does not appear to work.

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}$'