Nov-13-2021, 10:49 PM
(This post was last modified: Nov-13-2021, 10:49 PM by deanhystad.)
The problem with only calling once appears to be related to returning None. I changed you callback to this and it is called for each keystroke.
I don't know how to write a regex that would match '5', '55', '555' and '555-' but not '55-', '5-' or '-'. This works.
def validatePhone(num): print('called') pattern=re.compile('\d{3}-\d{3}-\d{4}') ok = pattern.search(num) print('got here', num, ok) return True if ok else FalseTo verify I tried both of these for validate. Returning None behaves as you saw. Called only once then validation no longer used. Returning False runs each time the key is pressed.
def validateFalse(num): print(num) return False def validateNone(num): print(num) return NoneSo your validation function needs to return True or False. But you still have a problem. The only way you can enter a phone number is using copy/paste. Typing will never match your pattern because 1 digit does not match your pattern and typing in "555-867-5309" requires you first type "5".
I don't know how to write a regex that would match '5', '55', '555' and '555-' but not '55-', '5-' or '-'. This works.
def validatePhone(num): pattern = 'ddd-ddd-dddd' if len(num) > len(pattern): return False for digit, chr in zip(num, pattern): if chr == 'd': if not digit.isdigit(): return False elif digit != chr: return False return True