How to Check that a Password Has At Least 1 Digit and 1 Uppercase Character in Python 


               


How to Check that a Password Has At Least 1 Digit and 1 Uppercase Character in Python



Python


In this article, we show how to check that a password has at least 1 digit and 1 uppercase character in Python.

So we will first show the Python code in order to check if the password has at least 1 digit and 1 uppercase character.

After this, we will show how to validate a password in Django to make sure it has at least 1 digit and 1 uppercase character.

So how we validating a password using regular expressions, the re module.

Using the re.search() function, we can search the password that the user has entered and see if it contains a digit and an uppercase character.


re.search() Function to Find a Digit in the Password

So we now show how to check if a password contains a digit.

The following code checks to see if the password contains a digit or not.



So the first thing we do is import the re module.

The re module is the module for regular expressions in Python.

We create a variable called password and set it equal to an input so that a user can enter in a password.

We then use an if statement to check to see if the password entered in contains a digit.

The re.search() function searches a string for a particular character or characters. We specify r"[\d]+ which means that the function looks for 1 or more digits in the string.

If this is not found, we print out, "This password must contain at least 1 digit"

If a digit is found, we print out, "This password is valid"

And this is how we can check to see if a password contains at least one digit or not.


re.search() Function to Find an Uppercase Character in the Password

Next we now look to see if the password entered contains an uppercase character or not.

Again, we use the regular expression module and use the re.search() function to check the string.

The following code below checks to see if a password contains at least one uppercase character or not.



Of course we import the re module again to use regular expressions.

We then ask for a user to input a password.

We then use an if statement to check whether the password contains an uppercase character, A-Z. If it does not, we print out, "This password must contain at least 1 uppercase character'. If it does, we print out that the password is valid.

We could combine both if statements together, but it's better to keep them separate so that you know what a user is missing (if missing something).


Django Framework

Basically if you are deploying this code on a website such as through a framework such as Django, you would put this code in a validators.py file and then run this function in the models.py file.

These would be the functions in the validators.py file.



So we have to import re.

The validate_password_digit() function checks to see if there is at least 1 digit present in the password. If not, we raise a ValidationError.

The validate_password_uppercase() function checks to see if there is at least 1 uppercase character in the password. If not, we raise a ValidationError.

We then run these functions in either the models.py file or the forms.py file, depending on which has the user input. If you doing a ModelForm, then it would be the models.py file.

This is what we have below using a models.py file.



So we first import these validating functions into the models.py file.

We then specify in the validators attribute of the text box the validating functions.

This allows the passwords entered by users to be validated to make sure the passwords have at least 1 digit and at least 1 uppercase character.

And this is how to check that a password has at least 1 digit and 1 uppercase character in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...