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
#10
(Jun-12-2024, 08:01 PM)deanhystad Wrote: One at a time is fairly standard. As a user I don't want a message 10 lines long telling me everything I did wrong.

You can only raise one validation error, but the message can be as long as you want. Check evrything, building your message for each problem, and if the message isn't empty, raise the ValidationError(composit_message).
    def validate_np(form, field):
        errors = []
        if form.un.data:
            if field.data in form.un.data.split("."):
                errors.append('New password cant contain firstname or lastname')
        if field.data == form.op.data:
            errors.append('New password cant match Current password')
        if len(field.data) < 12:
            errors.append('New password must be at least 12 characters')
        if not re.search(r"[0-9]", field.data):
            errors.append('New password has to contain one number')
        if not re.search(r"[a-z]", field.data):
            errors.append('New password has to contain one lower case character')
        if not re.search(r"[A-Z]", field.data):
            errors.append('New password has to contain one upper case character')
        if not re.search(r"[\`\¬\!\"\£\$\%\^\&\*\(\)\-\_\=\+\\\|\[\]\;\'\#\,\.\/\{\}\:\@\~\<\>\?]", field.data):
            errors.append('New password has to contain one special character')
        if not field.data == form.cnp.data:
            errors.append('New password has to match Confirm new password')
        if errors:
            ValidationError("\n".join(errors))
You could also block messages.
def validate_np(form, field):
    if form.un.data:
        if field.data in form.un.data.split("."):
            raise ValidationError("Password cannot contain your first or last name")
    if field.data == form.op.data:
        raise ValidationError("New and old passwords cannot be the same.")
    if re.search(r"\s", field.data):
        raise ValidationError("Passwords cannot contain spaces.")
    if len(field.data) < 12:
        raise ValidationError("Passwords must be 12 or more characters long")

    # These are character type checks
    errors = []
    if not re.search(r"[a-z]", field.data):
        errors.append("lower case characters")
    if not re.search(r"[A-Z]", field.data):
        errors.append("upper case characters")
    if not re.search(r"[0-9]", field.data):
        errors.append("digits")
    if not re.search(r"[\`\¬\!\"\£\$\%\^\&\*\(\)\-\_\=\+\\\|\[\]\;\'\#\,\.\/\{\}\:\@\~\<\>\?]", field.data):
        errors.append("special characters")
    if errors:
        print(errors)
        if len(errors) > 1:
            message = f"Passwords must contain {', '.join(errors[:-1])} and {errors[-1]}."
        else:
            message = f"Passwords must contain {errors[0]}."
        raise ValidationError(message)

    if not field.data == form.cnp.data:
        raise ValidationError("Passwords do not match")
I think this message is misleading:
Quote:New password has to contain one upper case character
The validation checks if there are more than zero upper case characters, not if there is one upper case character. A better message:
Quote:New password must contain upper case characters
I would also leave off things like "New" and "Confirm"

thanks deanhystad i appreciate it!

ive noticed for "cant contain firstname or lastname" lets say you put "admin" in the username and in new password put in "admini" this is allowed

does my front end coding look ok to you

do i need this in it to make it secure

{{ form.csrf_token }}

thanks,
rob
Reply


Messages In This Thread
RE: Regex to catch what user has put in text box - by robertkwild - Jun-13-2024, 08:39 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,576 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