Reputation: 9541
I have seen several questions on tkinter entry validation here but each one seems to stick to validate="key"
option.
While this is great for interactive validation, what i want is a "focusout"
validation.
More particularly I am looking to validate an email field. Here's the code I have tried so far but it doesn't work.
import Tkinter as tk
import re
master = tk.Tk()
def validateEmail(P):
x = re.match(r"[^@]+@[^@]+\.[^@]+", P)
return (x != None)
vcmd = (master.register(validateEmail), '%P')
emailentry = tk.Entry(master, validate="focusout", validatecommand=vcmd)
emailentry.pack()
b = tk.Button(master, text="Login")
b.pack()
tk.mainloop()
Any ideas on how to validate email entry please ?
Upvotes: 1
Views: 3982
Reputation: 386342
%S
represents the string being inserted, if any. This is only meaningful for validation on text insertion. When the widget loses focus, no character is being inserted so this parameter will always be an empty string. Since it is an empty string, it will always fail your validation.
You should use %P
instead, which represents the whole string.
Also, strictly speaking, the validation function should return a boolean rather than an object. You should save the result of the match in a variable, then return something like return (match is not None)
Upvotes: 2