Categories
Python

Custom Input Validation Python Inputs with PyInputPlus

Spread the love

ython is a convenient language that’s often used for scripting, data science, and web development.

In this article, we’ll look at how to do custom input validation with PyInputPlus.

Passing a Custom Validation Function to inputCustom()

We can use the inputCustom function to validate any input by users,

It accepts a single string argument of what the user entered, raises an exception if the string fails validation, returns None if inputCustom should return the string unchanged, and returns non-None values if inputCustom should return a different string from the one that’s entered.

The function is passed in as the first argument to inputCustom .

For instance, we can use it as follows:

import pyinputplus  
import re  
def check_phone(phone):    
  if bool(re.match(r'\d{3}-\d{3}-\d{4}', phone)) == False:  
    raise Exception('Invalid phone number')  
  return phoneprint('What is your phone number?')  
phone_num = pyinputplus.inputCustom(check_phone)  
print('Your number is', phone_num)

In the code above, we defined the check_phone function, which checks if the entered phone number is a valid phone number or not. If it isn’t, then it raises an exception.

Otherwise, we return the phone number.

Then we pass in the check_phone function to the inputCustom function.

When we run the program, then we can’t get past the prompt until we entered a valid phone number.

Otherwise, we get an error.

Whatever we return in check_phone is returned by inputCustom if validation succeeds.

Creating a Menu with inputMenu

We can use the inputMenu function to create a simple menu. It takes a list of options as strings. Also, we can set the lettered option to add letters to our choices if set to True . We can add numbers to our menu by setting the numbered option to False .

For instance, we can write the following to create a simple menu:

import pyinputplusfruit = pyinputplus.inputMenu(['apple', 'banana', 'orange'], lettered=True, numbered=False)  
print('Chosen fruit', fruit)

Then we can type in a letter to make a choice.

Narrowing Choices with inputChoice

We can use the inputChoice function to allow users to enter a value. The only valid inputs are the choices that we set.

For instance, we can write:

import pyinputplusfruit = pyinputplus.inputChoice(\['apple', 'banana', 'orange'\])  
print('Chosen fruit', fruit)

Then we get:

Please select one of: apple, banana, orange

displayed on the screen.

Once we entered one of the answers listed above, we can proceed.

Conclusion

We can use the inputCustom function to validate any input we want. It takes a validation function as an argument.

The function we pass in takes the input value as a parameter.

It should raise an exception if the input fails validation. Otherwise, it should return the value that’s entered or something derived from it.

inputCustom returns whatever it’s returned with the validation function.

We can create a menu with the inputMenu function and create an input that only accepts a few choices with the inputChoice function.

Leave a Reply

Your email address will not be published. Required fields are marked *