Advertisement
ChristianCury

StrongPassword Regex Validation

Dec 13th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const strongPassword = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
  2.  
  3.  
  4.  
  5. function likeGithubStyle(password) {
  6.   const hasLetters = new RegExp("^(?=.*[A-Za-z])")
  7.   const hasNumber = new RegExp("^(?=.*[0-9])")
  8.   const hasSize = new RegExp("^(?=.{8,})")
  9.   if (!hasLetters.test(password)) return {error: true, message: 'Missing letters'}
  10.   if (!hasNumber.test(password)) return {error: true, message: 'Missing numbers'}
  11.   if (!hasSize.test(password)) return {error: true, message: 'Missing size'}
  12.   return {error: false, message: 'OK'}
  13. }
  14.  
  15. function passwordIsStrong(password) {
  16.   const strongPassword = new RegExp("^(?=.*[a-zA-Z])(?=.*[0-9])(?=.{8,})");
  17.   return strongPassword.test(password)
  18. }
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. const hasLowercase = new RegExp("^(?=.*[a-z])")
  27. const hasUppercase = new RegExp("^(?=.*[A-Z])")
  28. const hasNumber = new RegExp("^(?=.*[0-9])")
  29. const hasSpecialchar = new RegExp("^(?=.*[!@#\$%\^&\*])")
  30. const hasSize = new RegExp("^(?=.{8,})")
  31.  
  32.  
  33. const password = 'bananaMaster123'
  34.  
  35. const result = strongPassword.test(password)
  36. const hasL = hasLowercase.test(password)
  37. const hasU = hasUppercase.test(password)
  38. const hasN = hasNumber.test(password)
  39. const hasSC = hasSpecialchar.test(password)
  40. const hasSI = hasSize.test(password)
  41.  
  42.  
  43.  
  44. console.log('Final result: ' + result)
  45.  
  46. console.log('lowercase: ' + hasL)
  47.  
  48. console.log('uppercase: ' + hasU)
  49.  
  50. console.log('number: ' + hasN)
  51.  
  52. console.log('special char: ' + hasSC)
  53.  
  54. console.log('size: ' + hasSI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement