Skip to content

Instantly share code, notes, and snippets.

@lmn3x
Last active August 23, 2023 05:45
Show Gist options
  • Save lmn3x/b36f20f15b597a08aee7a723a0fa8b7d to your computer and use it in GitHub Desktop.
Save lmn3x/b36f20f15b597a08aee7a723a0fa8b7d to your computer and use it in GitHub Desktop.
django validator for code meli
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