Python Forum
building functions to validate strings , date etc
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
building functions to validate strings , date etc
#1
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
Reply
#2
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.
Reply
#3
We also have a tutorial on validating user input.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 116 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 514 Jan-20-2024, 04:45 AM
Last Post: 1418
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  validate large json file with millions of records in batches herobpv 3 1,221 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 905 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Sad how to validate user input from database johnconar 3 1,837 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Date format and past date check function Turtle 5 4,066 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,190 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,802 Sep-03-2020, 02:25 PM
Last Post: NG0824

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020