How can I customise error messages shown by PyInputPlus in python? I have tried many method but unable to do it.
import pyinputplus as pyip
number = pyip.inputNum("Enter your phone number : ",
min=1000000000,
max=9999999999)
number
I want to print error message as please enter a valid 10 digit phone number. Is there any way to so it? I try to use "allowRegexes and blockRegexes" but unable to understand it.
If you are using this for a real world project I would recommend using input
from python itself, this lib doesn't seem very well documented and mantained. This could bring a lot of weird errors in your code for the future.
But to answer your question, you could do it using regex with the parameter blockRegexes. If you were unable to understand it, this will be more a regex question than a python question.
From this website you can learn a lot about regex, that I recommend, regex is a very important tool to understand. About your problem, accordingly to the docs:
blocklistRegexes
(Sequence, None): A sequence of regex str or(regex_str, error_msg_str)
tuples that, if matched, will explicitly fail validation.
So, in your case the first item in the tuple, should be a regex to block everything that have more or less than 10 integers characters:
^\d{10}$
The full explanation for this regex can be found here
The second item in your touple should be the string you want to appear when the error occurs:
"please enter a valid 10 digit phone number"
So your code would be like this:
number = pyip.inputNum("Enter your phone number : ",
min=1000000000,
max=9999999999,
blockRegexes=[(r"^\d{10}$","please enter a valid 10 digit phone number")])