Format check on date of birth in Python
Try something like this:
def validate(self): dateofbirth = self.DOBEntry.get() try: datetime.datetime.strptime(dateofbirth, '%D/%M/%Y') return True # returns a boolean True except ValueError: tkinter.messagebox.showerror("Error","Please enter the date of birth in the correct format DD-MM-YYYY") return Falsedef AddCustomer(self): # Same code if self.validate(): # If true is returned, then...also same as if self.validate()==True try: with sqlite3.connect("LeeOpt.db") as db: cursor = db.cursor() add_customer = ('''INSERT INTO Customer(CustomerID, BranchID, Name, Surname, DateOfBirth, Town, Postcode, EmailAddress, TelephoneNo, MedicalConditions) VALUES (?,?,?,?,?,?,?,?,?,?)''') cursor.execute(add_customer, (customerid,branchid,name,surname,dateofbirth,town,postcode,email,telephone,medical)) tkinter.messagebox.showinfo("Notification","Customer added successfully") self.ClearEntries() except (sqlite3.IntegrityError): tkinter.messagebox.showerror("Error","This CustomerID is already taken, please try another.") self.ClearID()
Here True
will be returned only if there is no error in the line, else False
will be returned, so based on that, you make an if
inside AddCustomer()
. I also changed your parameters for execute
onto a tuple
rather than a list
as it is convention.