-2

I am new to Python Tkinter programming. Can someone please help me validate the phone number and email fields? As in the phone number field should accept exactly 10 digits. Email ID should be of the form like in G Mail.

from tkinter import *
import xml.etree.ElementTree as etree
def saveData():
        mobile = etree.SubElement(root,'MOBILE')
        mobile.text = entry_mobile.get()

        email = etree.SubElement(root,'EMAIL')
        email.text = entry_email.get()

        tree=etree.ElementTree(root)
        tree.write("data.xml")
        return


def createWidget():
        global entry_mobile,entry_email
        def testVal(inStr,i,acttyp):
            ind=int(i)
            if acttyp == '1': #insert
                if not inStr[ind].isdigit():
                    return False
            return True
        Label(data, text="DATA").grid(row=0)
        mobile = Label(data, text="Mobile")
        mobile.grid(row=1, sticky=E)
        entry_mobile = Entry(data, bg="powder blue", validate="key")
        entry_mobile['validatecommand'] = (entry_mobile.register(testVal),'%P','%i','%d')
        entry_mobile.grid(row=1, column=1)

        email = Label(data, text="Email ID")
        email.grid(row=2, sticky=E)
        entry_email = Entry(data, bg="powder blue")
        entry_email.grid(row=2, column=1)
        submit = Button(data,text="Save",command = saveData) #binding a function to a widget
        submit.grid(column=1)
        return

data= Tk()
createWidget()
root = etree.Element("DATA")
data.mainloop()
Palbar
  • 1
  • 4
  • 1
    What kind of help do you need? Just asking us to fix any bugs we might find is off topic here. – Bryan Oakley May 19 '17 at 18:48
  • Actually, we want to validate the text that we enter in the entry widget. I want that if the user enters less than or more than 10 digits in the mobile number field, it should say error. I tried working with regular expressions but couldn't solve it – Palbar May 23 '17 at 13:59
  • The entry widget should accept exactly 10 digit and the same should be saved in the xml file. Please we need some help. – Palbar May 23 '17 at 14:05

2 Answers2

2

Use regular expressions in the validation function.

import re

phonere = re.compile(r'^[0-9]{1,10}$')

The expression ^[0-9]{1,10}$ means that there should be between 1 and 10 digits between the beginning and end of the string.

def is_phone(data):
    return phonere.match(data) != None

If the data matches, the match method returns a SRE_match object. Otherwise it returns None. So if the return of the match method is not none, we have a valid phone number.

vcmd = data.register(is_phone)
entry_mobile['validate'] = 'key'
entry_mobile['validatecommand'] = (vcmd, '%P')

We only need the %P to check the entry field data.

Edit: For e-mail addresses, you could use:

emailre = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")

according to this site.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • can you please tell me what 'data.register' does and what 'data' is? – Palbar May 23 '17 at 13:57
  • Well, `data` is the `Tk`instance I borrowed from your code. :-) And `register` is how you register a callback from tcl to Python. For the latter, see e.g. [here](http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.register-method). – Roland Smith May 23 '17 at 17:31
  • Ohh.. I got confused with my actual code and this sample code's Tk() object. Anyway thank you very much, It works. :D – Palbar May 23 '17 at 18:49
  • I tried both of this expressions for email validation but I'am not able to enter anything inside the particular entry widget. emailre = re.compile(r'(.*)@(.*?).com') AND emailre = re.compile(r'(.{,*})@(.{,*?}).com$') – Palbar May 23 '17 at 19:32
  • The entered email will be of form palbar09@gmail.com – Palbar May 23 '17 at 19:41
  • @Palbar I've added a regex for e-mail addresses. – Roland Smith May 23 '17 at 19:56
0

All you need to do to validate this is use string manipulation commands. What you need for this is to fit this into separate function inside your code:

if entry_mobile.isnumeric() == True:  #Confirms that all of the characters are numbers 
    if entry_email.find('@') != -1:
        if entry_email.find('.') != -1:
            if len(entry_mobile) == 10:
                return True
             else:
                 return False
         else:
             return False
     else:
         return False
 else:
     return False

If it returns true then it is valid by your standards and if it returns false then it is invalid. I hope this helps!

J Willets
  • 1
  • 1