I am using the auth module for the login process for my web2py application.I want to validate the password using regular expressions but couldn't find a way for it.How could we modify the access control to accept regex
1 Answers
In a model file (likely in db.py), you should have the following line, which defines all the Auth
tables, including db.auth_user
:
auth.define_tables()
When the db.auth_user
table is defined, the requires
attribute of its password
field is set to be a list containing a single CRYPT
validator. If you would like to apply a regular expression validation, you can do so by inserting an IS_MATCH
validator before the CRYPT
validator in the requires
attribute. To do this, anywhere after the above line, include the following:
password_is_match = IS_MATCH(r'your_regex', error_message='Your error message',
search=True)
db.auth_user.password.requires.insert(0, password_is_match)
Note, if you do not set search=True
, the validator will prepend your regex with a "^".
Also, note that the reason for inserting the IS_MATCH
validator before the CRYPT
validator is that the CRYPT
validator transforms the password by hashing it, so any validators applied after the CRYPT
validator will receive the hashed password rather than the original.

- 25,466
- 3
- 28
- 57
-
where would I have the list of validators in web2py so that I could insert this line – radha Mar 17 '16 at 18:22
-
if you haven't change anything this should be inside model db.py – Vladimir S. Mar 18 '16 at 13:15
-
Hopefully the edited answer clarifies things. Just use the code as shown in the answer -- you do not have to find any list of validators anywhere. – Anthony Mar 18 '16 at 16:18