Python Forum
Regex to catch what user has put in text box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex to catch what user has put in text box
#7
(Jun-11-2024, 05:28 PM)deanhystad Wrote: You should not be using multiple validators like that. Write a validator function for password and do all the validation using normal Python code.

This looks terrible:
    np = PasswordField('New Password', [InputRequired(message='please enter your new password'), EqualTo('cnp', message='must match confirm new password'), Length(min=12), Regexp('.*[a-z]', message='must contain one lower case'), Regexp('.*[A-Z]', message='must contain one upper case'), Regexp('.*[0-9]', message='must contain one number'), Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]', message='must contain one special character')])
You it up quite a bit.
    new_password = PasswordField('New Password', [
        InputRequired(message='please enter your new password'),
        EqualTo('cnp', message='must match confirm new password'),
        Length(min=12),
        Regexp('.*[a-z]', message='must contain one lower case'),
        Regexp('.*[A-Z]', message='must contain one upper case'),
        Regexp('.*[0-9]', message='must contain one number'),
        Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]', message='must contain one special character')]
    )
Or better yet, write a validator function.
class PasswordForm(FlaskForm):
    username = StringField('Username', [InputRequired(message='please enter your Username')])
    old_password = PasswordField('Current Password', [InputRequired(message='please enter your current password')])
    new_password = PasswordField('New Password', [InputRequired(message='please enter your new password')])
    confirm_password = PasswordField('Confirm New Password')


    def validate_new_password(form, field):
        """Validate password field."""
        if field.data == form.old_password.data:
            raise ValidationError('new password cannot be the same as current password.')
        if field.data != form.confirm_password.data:
            raise ValidationError('passwords do not match.')
        if len(field.data < 12):
            raise ValidationError('must be at least 12 characters long')
        if re.match(r"[A-Z]", field.data) is None:
            raise ValidationError('must contain an upper case letter.')
        if re.match(r"[a-z]", field.data) is None:
            raise ValidationError('must contain a lower case letter.')
        if re.match(r"[0-9]", field.data) is None:
            raise ValidationError('must contain a digit.')
        if re.match(r"[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]", field.data) is None:
            raise ValidationError('must contain a special character.')
        if form.username.data is not None:
            if field.data in form.username.data.split():
                raise ValidationError('cannot be your first or last name.')
Beware, untested code.

ok so use custom validation options, thanks a lot, i will look into this as your right, is there a really good how to i could get all this info from and learn it
Reply


Messages In This Thread
RE: Regex to catch what user has put in text box - by robertkwild - Jun-12-2024, 11:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,619 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  try catch not working? korenron 2 1,004 Jan-15-2023, 01:54 PM
Last Post: korenron
  python-docx regex: replace any word in docx text Tmagpy 4 2,478 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Multiprocessing queue catch get timeout Pythocodras 1 2,575 Apr-22-2022, 06:01 PM
Last Post: Pythocodras
  twisted: catch return from sql wardancer84 0 1,609 Sep-08-2021, 12:38 PM
Last Post: wardancer84
  how to catch schema error? maiya 0 1,974 Jul-16-2021, 08:37 AM
Last Post: maiya
  is this a good way to catch exceptions? korenron 14 5,084 Jul-05-2021, 06:20 PM
Last Post: hussaind
  pool mysql error - not catch by try\except? korenron 1 2,266 Jul-05-2021, 11:26 AM
Last Post: ibreeden
  Regex text file to store data in list TheSithSiggi 1 1,638 Dec-03-2020, 04:46 PM
Last Post: bowlofred
  try catch question ,get data from main code korenron 7 3,429 Nov-03-2020, 09:28 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020