Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 building functions to validate strings , date etc
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 ?
scidam wrote Aug-09-2019, 12:10 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
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.
We also have a tutorial on validating user input.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Validate JSON file BellaMac 12 739 Feb-27-2020, 03:17 PM
Last Post: snippsat
  convert strings of date to datetime exported from CSV GiorgosPap31 1 478 Oct-31-2019, 02:37 PM
Last Post: buran
  Substracting today's date from a date in column of dates to get an integer value firebird 1 467 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  Finding multiple strings between the two same strings Slither 1 542 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to change existing date to current date in a filename? shankar455 1 580 Apr-17-2019, 01:53 PM
Last Post: snippsat
  Python validate excel values data types Useruser00 0 1,510 Apr-08-2019, 01:29 PM
Last Post: Useruser00
  I want to validate that there is not more than two blank spaces in a string of charac jlpavon1987 4 606 Mar-29-2019, 10:49 PM
Last Post: woooee
  Date format conversion "/Date(158889600000)/" lbitten 2 616 Nov-29-2018, 02:14 PM
Last Post: Larz60+
  lists, strings, and byte strings Skaperen 2 1,549 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  cross validate amilie1234 6 3,577 Feb-09-2017, 08:28 PM
Last Post: amilie1234

Forum Jump:

Users browsing this thread: 1 Guest(s)