Guest User

Untitled

a guest
Jun 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. /**
  2. * Validate password strength.
  3. *
  4. * {{{
  5. * case class Model(password: String)
  6. *
  7. * val form: Form[Model] = {
  8. * import play.api.data.Forms._
  9. *
  10. * Form(
  11. * mapping(
  12. * "password" -> password
  13. * )(Model.apply)(Model.unapply)
  14. * )
  15. * }
  16. * }}}
  17. */
  18. val password: Mapping[String] = text(minLength = 8, maxLength = 32).verifying(
  19. "Password must contain at least 3 of the following: capital letter, lowercase letter, number, or symbol",
  20. value => {
  21. val codePoints: Seq[Int] = value.codePoints().toArray
  22.  
  23. val categorizers: List[Int => Boolean] = List(
  24. (codePoint: Int) => Character.isUpperCase(codePoint),
  25. (codePoint: Int) => Character.isLowerCase(codePoint),
  26. (codePoint: Int) => Character.isDigit(codePoint),
  27. (codePoint: Int) => !Character.isLetterOrDigit(codePoint)
  28. )
  29.  
  30. val count = categorizers.foldLeft(0) { (sum, categorizer) =>
  31. sum + (if (codePoints.exists(categorizer)) 1 else 0)
  32. }
  33.  
  34. count >= 3
  35. }
  36. )
Add Comment
Please, Sign In to add comment