I use python 2.7.12 with pymongo 2.8.1, django 1.11.7, mongoengine 0.9.0 and mongodb 3.4.10. I am creating a custom user model which inserts documents in a regular mongodb collection. I inherited the 'Document' class from mongoengine to create the string password field. I've been trying to create a regex pattern for the following conditions:
I tried using re.compile() in the following way:
import re
regexp = re.compile('[A-Za-z0-9@#$%^&+=]', max_length=25, min_length=8)
password = StringField(regex=regexp)
I sent HTTP POST requests with invalid passwords and it only throws an error when the length is lesser than 8 or greater than 25, otherwise they pass. I'm new to mongoengine and I'm not really sure what kind of input the regex parameter takes, neither have I been able to find examples for StringField. So what am I doing wrong?
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[@#$%^&+=]).{8,25}$
You can try this.Each condition is added using lookahead.Add more special characters if you want.
See demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With