from PyInquirer import prompt, Validator, ValidationError from prompt_toolkit import document import regex class PhoneValidator(Validator): def validate(self, document: document.Document) -> None: ok = regex.match('^\+?\d[\d ]+\d$', document.text) if not ok: raise ValidationError(message = 'Please enter a valid phone number', cursor_position = len(document.text)) widget = [ { 'type':'input', 'name':'number', 'message':'Type your phone number', 'validate':PhoneValidator } ] try : result = prompt(widget) except ValueError : print('Pb !!!') exit() print('Your answer is') print(result["number"]) print(type(result["number"]))