javascript - Regex to match multiple patterns in any order -


i'm validating password complexity in asp.net mvc3 app. current requirements must contain @ least 1 upper case letter, 1 lower case letter, 1 digit , no more 3 repeated characters. i'd generalise numbers though, , add condition non-alphanumeric characters.

at present, i'm validating server-side only, i'm able call regex.ismatch multiple times using 1 regex each condition. want able validate client-side though. because unobtrusive jquery validation allow 1 regex, need combine 5 conditions single pattern.

i don't know when comes regular expressions i've been doing bit of reading recently. may missing simple can't find way , multiple patterns way | or them.

you can (in .net) several lookahead assertions in single regex:

^(?=.*\p{lu})(?:.*\p{ll})(?=.*\d)(?=.*\w)(?!.*(.).*\1.*\1) 

will match if conditions true.

^                  # match start of string (?=.*\p{lu})       # true if there @ least 1 uppercase letter ahead (?=.*\p{ll})       # true if there @ least 1 lowercase letter ahead (?=.*\d)           # true if there @ least 1 digit ahead (?=.*\w)           # true if there @ least 1 non-alnum character ahead (?!.*(.).*\1.*\1)  # true if there no character repeated twice ahead 

note match not going consume characters of string - if want match operation return string you're matching against, add .* @ end of regex.

in javascript, can't use unicode character properties. instead use

^(?=.*[a-z])(?:.*[a-z])(?=.*\d)(?=.*\w)(?!.*(.).*\1.*\1) 

which of course use ascii letters validation. if that's ok you, fine. go , augment character classes [a-zÄÖÜÀÈÌÒÙÁÉÍÓÚ] etc. etc. never complete this. on server side, if want validation yield same result, you'd have specify regexoptions.ecmascript .net regex engine behaves javascript engine (thanks alan moore noticing!).


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -