Telephone number type

While we're at it, since it is a regularly occurring field in forms, how  
about adding a telephone number type?

I went looking for suitable standards to reference, and I found this:

 E.123 : Notation for national and international telephone numbers, e-mail  
addresses and web addresses

This is actually about how to represent telephone numbers on printed  
materials, but seems usable.

It doesn't give a formal syntax, only examples and descriptions, from  
which I extract the following syntax:

   telephone: international | local.
   international: "+" digits+
   local: prefix? digits+
   prefix: "(" digits+ ")"
   digits: digit (spacing digit)?
   digit: ["0"-"9"]
   spacing: " " | "-"

However, looking at
some countries seem to expect to be able to bracket the area code even in  
an international number, so we should be a bit laxer; for instance:

   telephone: prefix? area? digits+
   prefix: "+" digits+
   area: "(" digits+ ")"
   digits: digit (spacing digit)?
   digit: ["0"-"9"]
   spacing: " "+ | "-"

I think this regexp covers it:

 "+"? digs+ ("(" digs+ ")" digs+)?
 digs: [0-9] ("-" [0-9])?

and after every terminal there may follow spaces.

That produces this monstrosity:

   <pattern value="^\ *\+?([0-9]\ *(\-\ *[0-9]\ *)?)+(\(([0-9]\ *(\-\  
*[0-9]\ *)?)+\)\ *([0-9]\ *(\-\ *[0-9]\ *)?)+)?\ *$"/>

which you can try out here (scroll to the bottom):

Let me know if you find cases that don't work.


Received on Tuesday, 12 September 2017 09:43:37 UTC