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 - 2010 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
)
73 if val
== '' and not self
.empty
:
78 elif self
.pause
and MATCHER_PAUSE
.match(val
) == None:
80 elif not self
.pause
and MATCHER_NORMAL
.match(val
) == None:
82 elif immediate
and val
== '+':
86 def Validate(self
, win
= None):
87 textcontrol
= self
.GetWindow()
88 val
= textcontrol
.GetValue()
90 result
= self
.CheckText(val
)
92 if not result
and win
!= None:
94 _('You did not specify valid phone number.'),
95 _('Invalid phone number'),
96 wx
.OK | wx
.ICON_WARNING
).ShowModal()
97 textcontrol
.SetFocus()
101 def OnChar(self
, event
):
102 key
= event
.GetKeyCode()
105 if (key
< wx
.WXK_SPACE
or
106 key
== wx
.WXK_DELETE
or
110 event
.ControlDown() or
117 textcontrol
= self
.GetWindow()
118 pos
= textcontrol
.GetInsertionPoint()
119 val
= textcontrol
.GetValue()
120 newval
= val
[0:pos
] + char
+ val
[pos
:len(val
)]
121 if self
.CheckText(newval
, immediate
= True):
124 except UnicodeDecodeError:
128 if not wx
.Validator_IsSilent():
131 # Returning without calling even.Skip eats the event before it
132 # gets to the text control