Issue
I am working on a piece of code that checks for valid user credentials, as so in my app:
@app.route('/register', methods=('GET', 'POST'))
def register():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
db = get_db()
error = None
if not username:
error = 'Username is required'
if validate_user(username) is False:
error = 'Usernames must be 5 to 10 in length, not start with a number and use only lowercase.'
elif not email:
error = 'Email is required'
elif not password:
error = 'Password is required'
in the function for validation of the user, here is the code:
def validate_user(user):
valid_user = re.compile(r'\D{1}[D\d]{4,9}')
matched = valid_user.match(user)
if matched:
return True
else:
return False
in my form using jinja i have the following code, specific to the user part:
{% block content %}
<form method="post">
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username" size="45"
title="cannot start with a number, have a length of 5 to 10 non-special characters" required>
</div>
As I am testing this function the page doesn't go past the registration even though everything seems in place. Really spinning my wheels on this, as it seems so trivial...
Solution
This pattern \D[D\d]{4,9}
matches any char except a digit, then 4-9 times either a D
char or a digit. It is also unanchored without $
, to the maximum length to have re.match return a match object is not 10
If you want for the username:
Usernames must be 5 to 10 in length, not start with a number and use only lowercase.
\D[^A-Z]{4,9}$
Note that \D
and [^A-Z]
can also match spaces and newlines.
If those are not allowed, you can exclude \s
from the range using a negated character class. As you are using re.match in the code, you can omit the ^
anchor.
[^\s\d][^\sA-Z]{4,9}$
Answered By - The fourth bird
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.