phone_number.py 654 B

12345678910111213141516171819202122232425262728
  1. from PyInquirer import prompt, Validator, ValidationError
  2. from prompt_toolkit import document
  3. import regex
  4. class PhoneValidator(Validator):
  5. def validate(self, document: document.Document) -> None:
  6. ok = regex.match('^\+?\d[\d ]+\d$', document.text)
  7. if not ok:
  8. raise ValidationError(message = 'Please enter a valid phone number', cursor_position = len(document.text))
  9. widget = [
  10. {
  11. 'type':'input',
  12. 'name':'number',
  13. 'message':'Type your phone number',
  14. 'validate':PhoneValidator
  15. }
  16. ]
  17. try :
  18. result = prompt(widget)
  19. except ValueError :
  20. print('Pb !!!')
  21. exit()
  22. print('Your answer is')
  23. print(result["number"])
  24. print(type(result["number"]))