Re: Validating GB telephone numbers in Django forms.

441 views
Skip to first unread message

g1smd.1

unread,
Sep 4, 2012, 2:15:12 PM9/4/12
to django...@googlegroups.com
On Monday, September 3, 2012 8:10:46 PM UTC, g1smd.1 wrote:
I see that there are routines for validating telephone numbers in forms in Django for several countries.

The code for that can usually be found in the forms.py file located in the various country folders here:
https://github.com/django/django/tree/master/django/contrib/localflavor

So far, there is nothing for GB numbers, here:
https://github.com/django/django/tree/master/django/contrib/localflavor/gb

I've written a bunch of RegEx patterns to get this functionality started. The patterns are 100% fully tested. All that's needed is a few lines of python logic to string them together. The details can be found at:
https://github.com/django/django/pull/316/files

My python foo is almost zero. Anyone care to have a go at getting this to work?

RegEx 1 checks the user entered something that looks like a GB telephone number:
020  3000  5555
02075  567  234
0114  223  4567
01145  345  567
+44  1213  456  789
00  44  (0)  1697  73555
011  44  11  4890  2345
and several other formats, without worrying if the format is correct for this particular number (but read on). It allows for national or international format, even for two common international dial prefixes. What is most important is that the user enters the right number of digits. Don't constrain the user to use a particular format for entry.
"Be lenient in what you accept, be strict in what you send." (Postel's Law)

RegEx 2 extracts the NSN part of the number in $3, with "44" or NULL in $2 (so you know if international or national format was used on input), and any extension in $4. Store $2 and $4 for later. Send $3 on to RegEx 3.

RegEx 3 tests the NSN part of the number is in a valid range and has the right number of digits for that range (GB numbers can be 9 or 10 digits long). This RegEx pattern is very detailed. You can say that a number is possible or is invalid with this RegEx.

RegEx Group 4. Here, there's a bunch of RegEx patterns that specify how a GB number should be formatted, detailed by number length and initial digits. These rules cover all GB numbers.

The last bit is to add the 0 or +44 back on, and append the original extension number (if present) and present it back to the user.

020  3000  5555 => Valid: 020  3000  5555
02075  567  234 => Valid: 020  7556  7234
0114  223  4567 => Valid: 0114  223  4567
01145  345  567 => Valid: 0114  534  5567
+44  1213  456  789 => Valid: +44  121  345  6789
00  44  (0)  1697  73555 => Valid: +44  16977  3555
011  44  11  4890  2345 => Valid: +44  114  890  2345
0623 111 3456 => NOT VALID




This is now also filed as ticket #18903 at https://code.djangoproject.com/ticket/18903




Amyth Arora

unread,
Sep 5, 2012, 3:01:43 AM9/5/12
to django...@googlegroups.com

you can alternatively use jquery to validate the field, i do not have the link handy as m on my phone right now but google validation engine, it is one of the most powerful jquery validation library which also has builtin method of validating uk phone numbers.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/h0iG7nJrXikJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

g1smd.1

unread,
Sep 5, 2012, 8:29:57 AM9/5/12
to django...@googlegroups.com

Thanks for the reply and information.


The jQuery validation routines for GB telephone numbers contain significant errors and shortcomings. I already posted some patches correcting many of those problems at least a month ago, but they haven't been reviewed yet.


The new Python/django telephone number validation code I am proposing here is far more comprehensive and has much more detailed range and length checking. Similar code already works fine in PHP and Java elsewhere, but I am having trouble converting it all to Python.

I have added some Python logic and code to the initial RegEx patterns that I posted yesterday, but I now need help to finish it off and get it working. I am not at all familiar with the Python syntax and the manual is somewhat terse and obtuse.

I had a problem with GitHub this morning, so the pull request is now at https://github.com/django/django/pull/324

The code so far, can be found at: https://github.com/g1smd/django/blob/master/django/contrib/localflavor/gb/forms.py

I'm guessing that someone proficient in Python could finish it off in a couple of hours or less.

Any volunteers?

Amyth Arora

unread,
Sep 5, 2012, 2:24:09 PM9/5/12
to django...@googlegroups.com
To be frank , i am not really familiar with all possibilities of GB phone numbers, if you could throw some light on this, i might be able to help ya.

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Zohb6P8AsaIJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Thanks & Regards
----------------------------

Amyth [Admin - Techstricks]
Email - aroras....@gmail.com, ad...@techstricks.com
Twitter - @a_myth_________
http://techstricks.com/

g1smd.1

unread,
Sep 5, 2012, 7:05:45 PM9/5/12
to django...@googlegroups.com

The GB number plan is quite complicated, with a variety of number lengths and formats.
Some of it is detailed here:
http://www.aa-asterisk.org.uk/index.php/Number_format

I already compiled all the RegEx patterns that are needed. Some are listed here:
http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers

I've already started the process of translating a fully working PHP routine (that I wrote from scratch) into python. There will be syntax errors in the python version as I don't understand all that much of the python language.

The python functionality so far....  https://github.com/django/django/pull/324/files
or
https://github.com/g1smd/django/blob/master/django/contrib/localflavor/gb/forms.py

The code using an array is probably not quite right, and I guess it would be better to return error messages instead of "false" in some places.

However, I'd guess that someone who knows what they were doing could get it working in an hour or two.



On Wednesday, September 5, 2012 6:24:42 PM UTC, Amyth wrote:
To be frank, I am not really familiar with all possibilities of GB phone numbers, if you could throw some light on this, I might be able to help ya.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages