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
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.
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.
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.