I'm trying to use a regex to make sure a password ...
# box-products
o
I'm trying to use a regex to make sure a password contains a uppercase, lowercase, number and symbol. I am using the following code:
Copy code
var result = validateModel(
			target = rc,
			constraints = {
				...
				"password" : {
					"required" : true,
					"regex" : "/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)/",
					"size": "6..255"
				},
				"passwordConfirmation" : { "required" : true, "sameAs" : "password" }
			}
		);
However no matter what I put in it gives the validator error:
The 'password' value does not match the regular expression: /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)/
. I tried testing my regex using a regex tester https://www.regextester.com/ and used the string
Ok12345!
and verified it was correct. Any ideas?
d
have you tried it without the forwards slashes (beginning and end)?
o
Yes I tried
"regex" : "(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)",
and also tried
"regex" : "/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)/i",
and the same thing happened. 😞
a
You're using a JS regular expression (and tester) but writing CFML code, which is a bit of a false start
Also yer regex doesn't actually do what you want anyhow: you aren't doing the length check. That pattern will also match stuff that's too long and too short.
I dunno how ColdBox does its regex validation, but this demonstrates proper testing of a CFML regex for your requirements: https://trycf.com/gist/3386f9fb47ba1414634083820ff451f3/acf2021?setupCodeGistId=816ce84fd991c2682df612dbaf1cad11&theme=monokai
The difference with mine is that I'm testing for each char type you need, plus I'm testing the length.
s
this wont work in cf because its using
?=
a positive lookahead
a
@Scott Steinbeck... err,,, maybe if they're using CF5 or lower that might be an issue. AFAIK CF has supported positive and negative lookaheads since CFMX6 when they went from [whatever engine they were using to on the C++ version of CFML] to Java and using Apache ORO for the engine (https://svn.apache.org/repos/asf/jakarta/oro/trunk/docs/api/org/apache/oro/text/regex/package-summary.html) And... like... I included a working example in CFML two comments above your one...
s
whoops, missed your example
o
The difference with mine is that I'm testing for each char type you need, plus I'm testing the length.
I'm using
"size": "6..255"
to check the length, should I remove it and go with the regex?
a
Like I said... a) I don't know how coldbox works; b) your regex doesn't do a length check; c) plus at least initially it was a JS regex, not a CFML one. From there it's down to you to test and report back.
d
@Ookma-Kyi use https://regexr.com/ and switch from Javascript to PCRE (Server) which CFML is compatible with. Top right of the app.
o
@Daniel Mejia Did that, that is returning Ok12345! as a match using the regex ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).$ . However the page still errors with the message The 'password' value does not match the regular expression: The 'password' value does not match the regular expression: ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).$ . Here is the validation I am using for the password field: "password" : {                     "required" : true,                     "regex" : "^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).$",                     "size": "6..255"                 }, I'm running out of ideas and yes I checked the output to make sure what I typed into the field matches what is being passed in.
@Adam Cameron
Like I said... a) I don't know how coldbox works; b) your regex doesn't do a length check; c) plus at least initially it was a JS regex, not a CFML one.
From there it's down to you to test and report back.
It seems to work if I use the exact regex
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{6,255}$
however if I try to remove the
{6,255}
it fails. I've tried the following variations with no success:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).$
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).
I can keep the
{6, 255}
and remove the
size
validator, just trying to keep my code consistent.
a
Right so yeah... use the code that works... don't use the code that doesn't work. Like I said... I don't know anything about ColdBox, so there's no point pinging me with questions about it.
o
Ok sorry about that
a
Did you do any investigation of yer own? Did you go look at what ColdBox is doing around the regex-checking / length-checking code?
o
Yes I found the model for regex, but can't seem to find the actual implementation. https://github.com/coldbox-modules/cbvalidation/blob/development/models/validators/RegexValidator.cfc