Last active
August 23, 2023 05:45
-
-
Save lmn3x/b36f20f15b597a08aee7a723a0fa8b7d to your computer and use it in GitHub Desktop.
django validator for code meli
This file contains hidden or 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.exceptions import ValidationError | |
def validate_meli_code(value: str): | |
if not len(value) == 10: | |
raise ValidationError("کد ملی باید ۱۰ رقم باشد.") | |
res = 0 | |
for i, num in enumerate(value[:-1]): | |
res = res + (int(num) * (10 - i)) | |
remain = res % 11 | |
if remain < 2: | |
if not remain == int(value[-1]): | |
raise ValidationError("کد ملی درست نیست") | |
else: | |
if not (11 - remain) == int(value[-1]): | |
raise ValidationError("کد ملی درست نیست") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment