Author: mark
Date: 2008-05-20 06:02:07 -0600 (Tue, 20 May 2008)
New Revision: 3436

Modified:
   FormEncode/trunk/formencode/validators.py
Log:
update of IPhoneNumberValidator to accept more phone number variations

Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py   2008-05-19 15:00:23 UTC (rev 
3435)
+++ FormEncode/trunk/formencode/validators.py   2008-05-20 12:02:07 UTC (rev 
3436)
@@ -1585,7 +1585,7 @@
             result = result + " ext.%s" % match.group(4)
         return result
 
-# from [EMAIL PROTECTED]:303]
+# from [EMAIL PROTECTED]:417]
 class IPhoneNumberValidator(FancyValidator):
 
     """
@@ -1603,6 +1603,8 @@
         '+49-555-4860-300'
         >>> p.to_python('0555-49924-51')
         '+49-555-49924-51'
+        >>> p.to_python('0555 / 8114100')
+        '+49-555-8114100'
         >>> p.to_python('0555/8114100')
         '+49-555-8114100'
         >>> p.to_python('0555 8114100')
@@ -1615,8 +1617,12 @@
         '+49-555-87182-96'
         >>> p.to_python('0555-2 50-30')
         '+49-555-250-30'
+        >>> p.to_python('0555 43-1200')
+        '+49-555-43-1200'
         >>> p.to_python('(05 55)4 94 33 47')
         '+49-555-49433-47'
+        >>> p.to_python('(00 48-555)2 31 72 41')
+        '+48-555-23172-41'
         >>> p.to_python('+973-555431')
         '+973-555431'
         >>> p.to_python('1-393-555-3939')
@@ -1629,6 +1635,10 @@
         '+218-55-3350317-321'
         >>> p.to_python('+218 (0)55-3636639/38')
         '+218-55-3636639-38'
+        >>> p.to_python('032 555555 367')
+        '+49-32-555555-367'
+        >>> p.to_python('(+86) 555 3876693')
+        '+86-555-3876693'
     """
 
     strip = True
@@ -1636,14 +1646,16 @@
     default_cc = None
     _mark_chars_re = re.compile(r"[_.!~*'/]")
     _preTransformations = [
-        (re.compile(r'^\((\d+)\s*(\d+)\)(.+)$'), '%s%s-%s'),
+        (re.compile(r'^(\(?)(?:00\s*)(.+)$'), '%s+%s'),
+        (re.compile(r'^\(\s*(\+?\d+)\s*(\d+)\s*\)(.+)$'), '(%s%s)%s'),
+        (re.compile(r'^\((\+?[-\d]+)\)\s?(\d.+)$'), '%s-%s'),
         (re.compile(r'^(?:1-)(\d+.+)$'), '+1-%s'),
-        (re.compile(r'^00\s*(\d+.+)$'), '+%s'),
         (re.compile(r'^(\+\d+)\s+\(0\)\s*(\d+.+)$'), '%s-%s'),
-        (re.compile(r'^(0\d+)\s(\d+)$'), '%s-%s'),
+        (re.compile(r'^([0+]\d+)[-\s](\d+)$'), '%s-%s'),
+        (re.compile(r'^([0+]\d+)[-\s](\d+)[-\s](\d+)$'), '%s-%s-%s'),
         ]
     _ccIncluder = [
-        (re.compile(r'^0([1-9]\d*)\-(\d+.*)$'), '+%d-%s-%s'),
+        (re.compile(r'^\(?0([1-9]\d*)[-)](\d.*)$'), '+%d-%s-%s'),
         ]
     _postTransformations = [
         (re.compile(r'^(\+\d+)[-\s]\(?(\d+)\)?[-\s](\d+.+)$'), '%s-%s-%s'),
@@ -1671,12 +1683,16 @@
 
     def _to_python(self, value, state):
         self.assert_string(value, state)
+        try:
+            value = value.encode('ascii', 'replace')
+        except:
+            raise Invalid(self.message('phoneFormat', state), value, state)
+        value = self._mark_chars_re.sub('-', value)
+        for f, t in [('  ', ' '), ('--', '-'), (' - ', '-'), ('- ', '-'), (' 
-', '-')]:
+            value = value.replace(f, t)
         value = self._perform_rex_transformation(value, 
self._preTransformations)
-        value = self._mark_chars_re.sub('-', value)
         if self.default_cc:
             value = self._prepend_country_code(value, self._ccIncluder, 
self.default_cc)
-        for f, t in [('  ', ' '), ('--', '-'), (' - ', '-'), ('- ', '-'), (' 
-', '-')]:
-            value = value.replace(f, t)
         value = self._perform_rex_transformation(value, 
self._postTransformations)
         value = value.replace(' ', '')
         # did we successfully transform that phone number? Thus, is it valid?
@@ -1684,9 +1700,6 @@
             raise Invalid(self.message('phoneFormat', state), value, state)
         return value
 
-
-
-
 class FieldStorageUploadConverter(FancyValidator):
     """
     Handles cgi.FieldStorage instances that are file uploads.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to