Advertisement
horozov86

Validation of password 2

Mar 25th, 2024
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def validate_password(password):
  2.     if len(password) < 6:
  3.         raise ValidationError(_("Password must be at least 6 characters long."))
  4.  
  5.     has_letter = False
  6.     has_digit = False
  7.     has_underscore = False
  8.  
  9.     for char in password:
  10.         if char.isalpha():
  11.             has_letter = True
  12.         elif char.isdigit():
  13.             has_digit = True
  14.         elif char == '_':
  15.             has_underscore = True
  16.  
  17.     if not has_letter:
  18.         raise ValidationError(_("Password must contain at least one letter."))
  19.    
  20.     if not has_digit:
  21.         raise ValidationError(_("Password must contain at least one digit."))
  22.    
  23.     if not has_underscore:
  24.         raise ValidationError(_("Password must contain at least one underscore."))
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement