Guest User

Untitled

a guest
Nov 7th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. from wtforms import Form, ValidationError
  2. from wtforms import StringField, PasswordField
  3. from wtforms.validators import Length, InputRequired
  4. from werkzeug.datastructures import MultiDict
  5. import re
  6.  
  7. def is_proper_username(form, field):
  8. if not re.match(r"^\w+$", field.data):
  9. msg = 'Polje "{}" može imati samo slova i brojke'.format(field.name)
  10. raise ValidationError(msg)
  11.  
  12. class LoginForm(Form):
  13. username = StringField(u'Username:', [InputRequired(), is_proper_username,Length(min=3, max=40)])
  14. password = PasswordField(u'Password:', [InputRequired(), Length(min=5, max=12)])
  15.  
  16. @staticmethod
  17. def validate_password(form, field):
  18. data = field.data
  19. if not re.findall('.*[a-z].*', data):
  20. msg = 'Polje "{}" mora imati barem jedno malo slovo'.format(field.name)
  21. raise ValidationError(msg)
  22.  
  23. # has at least one uppercase character
  24. if not re.findall('.*[A-Z].*', data):
  25. msg = 'Polje "{}" mora imati barem jedno veliko slovo'.format(field.name)
  26. raise ValidationError(msg)
  27.  
  28. # has at least one number
  29. if not re.findall('.*[0-9].*', data):
  30. msg = 'Polje "{}" mora imati barem jedan broj'.format(field.name)
  31. raise ValidationError(msg)
  32.  
  33. # has at least one special character
  34. if not re.findall('.*[^ a-zA-Z0-9].*', data):
  35. msg = 'Polje "{}" mora imati barem jedan specijalan znak'.format(field.name)
  36. raise ValidationError(msg)
  37.  
  38. # testing our form
  39. form = LoginForm(MultiDict([('username', 'nikovrdoljak'), ('password', 'lL2m@msbb')]))
  40. print (form.validate())
  41. print (form.errors)
Add Comment
Please, Sign In to add comment