Python Forum

Full Version: Regex to catch what user has put in text box
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
admin is not 12 characters long and does not contain upper case letters, digits or special characters. Do you want to invalidate passwords that contain admin, like "Iamtheadministrator1! That is a different test.
    if form.un.data:
        if any(name in field.data for name in form.un.data.split()):
            raise ValidationError("Password cannot contain your first or last name")
(Jun-13-2024, 05:44 PM)deanhystad Wrote: [ -> ]admin is not 12 characters long and does not contain upper case letters, digits or special characters. Do you want to invalidate passwords that contain admin, like "Iamtheadministrator1! That is a different test.
    if form.un.data:
        if any(name in field.data for name in form.un.data.split()):
            raise ValidationError("Password cannot contain your first or last name")
lets say i put in "robert.wild" in the username

in the new password field if i type in "robert" then hit submit doesnt catch it if i type in "wild" in the new password field doesnt catch it

ive done it by just addint the "."

if any (name in field.data for name in form.un.data.split(".")):
how do i do a re.IGNORECASE as if you type it in capitals it doesnt catch it
Quote:how do i do a re.IGNORECASE as if you type it in capitals it doesnt catch it
What do you mean? Is this real re or wtf.regexp? For re, read this:

https://docs.python.org/3/library/re.html
(Jun-19-2024, 10:59 PM)deanhystad Wrote: [ -> ]
Quote:how do i do a re.IGNORECASE as if you type it in capitals it doesnt catch it
What do you mean? Is this real re or wtf.regexp? For re, read this:

https://docs.python.org/3/library/re.html

it for my wtf form

if form.un.data:
            if any (name in field.data for name in form.un.data.split(".", flags=re.I)):
                raise ValidationError('New password cant contain firstname or lastname')
basically if you type in your username in the new password it errors saying cant contain firstname or lastname but if they capitalise it, it lets them have that as a new password, i want to stop that
Write a custom validator and you can use any re features you want.
def validate_np(form, field):
        if form.un.data:
            if any (name in field.data.lower() for name in form.un.data.split(".")):
                raise ValidationError('New password cant contain firstname or lastname')
        if field.data.lower() == form.op.data.lower():
            raise ValidationError('New password cant match Current password')
this works im testing it so it lower cases the variables whether you put them in lower or upper case
def validate_un(form, field):
        if not field.data == form.un.data.lower():
            raise ValidationError('Username needs to be Lowercase')
    
    def validate_np(form, field):
        if form.un.data:
            if any (name in field.data.lower() for name in form.un.data.split(".")):
                raise ValidationError('New password cant contain firstname or lastname')
        if field.data.lower() == form.op.data.lower():
            raise ValidationError('New password cant match Current password')
        if len(field.data) < 12:
            raise ValidationError('New password must be at least 12 characters')
        if not re.search(r"[0-9]", field.data):
            raise ValidationError('New password has to contain one number')
        if not re.search(r"[a-z]", field.data):
            raise ValidationError('New password has to contain one lower case character')
        if not re.search(r"[A-Z]", field.data):
            raise ValidationError('New password has to contain one upper case character')
        if not re.search(r"[\`\¬\!\"\£\$\%\^\&\*\(\)\-\_\=\+\\\|\[\]\;\'\#\,\.\/\{\}\:\@\~\<\>\?]", field.data):
            raise ValidationError('New password has to contain one special character')
        if not field.data == form.cnp.data:
            raise ValidationError('New password has to match Confirm new password')
so username needs to be lower case

is it ok to define two def commands in same script, they wont get confused with eachother will they?
Custom validators are named after the field they validate. You can have as many validators as fields.
ok im getting there, i dont know if this next bit is possible or not

for there current password is it possible to validate it with a powershell command to make sure it is there current password

as my main goal is to run this powershell command on my python script

Set-ADAccountPassword -Identity un -OldPassword (ConvertTo-SecureString -AsPlainText "op" -Force) -NewPassword (ConvertTo-SecureString -AsPlainText "cnp" -Force)
Pages: 1 2 3