Created
May 21, 2020 06:03
-
-
Save peketamin/681d3f926d6e886cfddbad3264170397 to your computer and use it in GitHub Desktop.
Django: phone number (JP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.validators import ValidationError # type: ignore[attr-defined] | |
from django.core.validators import RegexValidator | |
## validators.py | |
phone_number_validator = RegexValidator( | |
regex=r"^[0-9-()+ ]{9,20}$", message="入力可能文字は半角の、数字、ハイフン、括弧のみ. 9文字以上, 20文字以下." | |
) | |
## models.py | |
class MyModel(Model): | |
phone = models.CharField( | |
"電話番号", | |
max_length=20, | |
validators=[ | |
MinLengthValidator(9), | |
MaxLengthValidator(20), | |
phone_number_validator, | |
], | |
default=None, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment