0

I need help coming up with a regular expression for javascript which has to verify a password.
The password (8 characters long, at least one non-letter character)
it would also be useful to make sure the user does not have spaces at the beginning or end

Wade Guest
  • 65
  • 1
  • 1
  • 7
  • 1
    Before asking, did you do a search for "Javascript password validation"? (This question gets asked a lot and there are many good answers.) – ridgerunner Nov 15 '13 at 02:31

2 Answers2

1

This is a pattern that will match what you asked for

pattern = /^(?=.*[^a-z])\S.{6}\S$/i;

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0

Depends how 'complicated' you want to get but I find either if the following useful:

/*
    No more than 4 same characters
    - One digit
    - One uppercase
    - One lowercse
    - One 'punctuation' mark
    - between 8 and 20 characters
*/                                  
re = /^(?!.*(.)\1{4})((?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[^\w\d\s])).{8,20}$/;

/*
    - One Digit
    - One lower case
    - One upper case
    - Maximum 2 repeating char
    - between 6 and 20 characters
*/                                  
re = /^(?!.*(.)\1{2})((?=.*[a-z])(?=.*[A-Z])(?=.*\d)).{6,20}$/;

These are not my regexp's as I found them on the web but can't remember where.