Python Forum
building functions to validate strings , date etc - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: building functions to validate strings , date etc (/thread-20388.html)



building functions to validate strings , date etc - metro17 - Aug-08-2019

import datetime
from datetime import datetime
n = 5
while n > 0:
    id = input("Please enter your ID: ")
    file = open("C:\\Users\\abcd\\efg\\" "%s.txt" % id, "w")
    name = input("Please provide your name: ")
    Doctor = input("Please provide the name of the doctor you would like to visit: ")
    AppointmentDate = input("Please provide the date you want to see the doctor - Format:MM/DD/YYYY Ex:12/25/2019: ")
    AppointmentTime = input("Please provide the time you want to see the doctor - Format:24hr Ex:13:00: ")
    EmailID = input("Please provide your email to confirm your appointment: ")
    PhoneNum = input("Please provide your phone number to receive confirmation message: ")
    MoreData = input("Do you need to make any new Appointments? y/n :")
    if MoreData != "y":
        n= -1
The above piece of code works fine as long as I enter data as requested.
But it fails when I just press enter as it it goes through to the next line.
It should not go to the next line unless the user enter the relevant data in the requested format.
I need to make this more fool proof.
Need to validate the input string data for : id,name,doctor,emailid
Need to validate the input integer data for PhoneNum
Need to validate AppointmentDate only in this format : MM/DD/YYYY and also the data should always be greater than current date/time.
Need to validate time only in this format 24 hrs : Ex 13:00

I would like to create functions which will be referred in the above code to go through validation whenever a user enters any data.
how can I achieve this ?
Thanks


RE: building functions to validate strings , date etc - Gribouillis - Aug-08-2019

Some time ago I posted a function named typed_input(). It repeatedly asks for user input with type validation and error message. I think it applies very well here.


RE: building functions to validate strings , date etc - ichabod801 - Aug-08-2019

We also have a tutorial on validating user input.