Write a quick password validator to make sure that the people that visit your site use appropriate passwords.
Respond with "VALID" if the string meets the requirements or "INVALID" if it does not.
Requirements:
More than 3 characters but less than 20.
Must contain only alphanumeric characters.
Must contain letters and numbers.
Examples
'Username123!' => INVALID
'123' => INVALID
'Username123' => VALID
Tests
'Username'
'IsThisPasswordTooLong'
'DEVCommunity'
Good luck!
This challenge comes from jhoffner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (12)
Please, please, please... do not set maximum lengths on passwords. [1]
It's 2020, you should be hashing [2] your passwords (so the output is constant length no matter what the input). Password strength is based on two things: entropy (randomness) and length. Since people are usually bad at entropy, let them enter long passwords or passphrases if they want to. Also it sucks for people with password managers to have to generate custom rules. Accept any character, even spaces (you're hashing the password, so you can even accept ';-- for that matter), for passphrases.
[1] If you really want to set one to avoid 1GiB passwords, put it at 256 characters, so that password managers can go wild.
[2] Yes, I mean hashing + salting, don't roll it yourself and use bcrypt.
I know this is a training exercise, but cargo cult security is real.
Nice idea but it's not always your choice.
Case in point, you're advocating using bcrypt when bcrypt itself can only support 72 characters maximum, anything larger is truncated.
Maybe archaic systems, and also, shorter lengths are easier to make test cases that are easy for inspection. Most programming challenges I know are usually short and sweet (unless we're dealing with huge data deliberately).
Python solution
Here is the detailed version of above,
Output,
A little more readable example that is explicit about the lazy evaluation (chaining
and
)A lazy JS implementation:
Rust
Imperative, because two predicates of
any
and one ofall
would be three iterations, and that is simply not acceptable.The lambda is only a lambda to save lines on the internet. Don't do such things.
Look at it go.
Python
My swift solution :
DEVCommunity
would be invalid because it's missing the numeric part ;)A quick JS function:
I think you missed rule 3. The password needs both alpha and numeric.