1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
5 Phone number validator.
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright © 2003 - 2009 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 MATCHER_NORMAL
= re
.compile('^[0-9*#+]+$')
30 MATCHER_PAUSE
= re
.compile('^[Pp0-9*#+]+$')
31 MATCH_SPLIT
= re
.compile('[\s;,]+')
33 def SplitNumbers(text
):
35 Splits text to list of phone numbers.
37 lst
= MATCH_SPLIT
.split(text
)
40 if len(lst
) > 0 and lst
[len(lst
) - 1] == '':
44 class PhoneValidator(wx
.PyValidator
):
46 Validator for phone numbers.
48 def __init__(self
, multi
=False, pause
=False, empty
=False):
49 wx
.PyValidator
.__init
__(self
)
53 self
.Bind(wx
.EVT_CHAR
, self
.OnChar
)
56 return PhoneValidator(self
.multi
, self
.pause
, self
.empty
)
58 def TransferToWindow(self
):
61 def TransferFromWindow(self
):
64 def CheckText(self
, text
, immediate
= False):
66 Verifies whether enterd text is correct.
69 values
= SplitNumbers(text
)
75 elif self
.pause
and MATCHER_PAUSE
.match(val
) == None:
77 elif not self
.pause
and MATCHER_NORMAL
.match(val
) == None:
79 elif immediate
and val
== '+':
83 def Validate(self
, win
= None):
84 textcontrol
= self
.GetWindow()
85 val
= textcontrol
.GetValue()
87 result
= self
.CheckText(val
)
89 if not result
and win
!= None:
91 _('You did not specify valid phone number.'),
92 _('Invalid phone number'),
93 wx
.OK | wx
.ICON_WARNING
).ShowModal()
94 textcontrol
.SetFocus()
98 def OnChar(self
, event
):
99 key
= event
.GetKeyCode()
102 if (key
< wx
.WXK_SPACE
or
103 key
== wx
.WXK_DELETE
or
107 event
.ControlDown() or
114 textcontrol
= self
.GetWindow()
115 pos
= textcontrol
.GetInsertionPoint()
116 val
= textcontrol
.GetValue()
117 newval
= val
[0:pos
] + char
+ val
[pos
:len(val
)]
118 if self
.CheckText(newval
, immediate
= True):
121 except UnicodeDecodeError:
125 if not wx
.Validator_IsSilent():
128 # Returning without calling even.Skip eats the event before it
129 # gets to the text control