最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Need a regular expression for an Irish phone number - Stack Overflow

programmeradmin4浏览0评论

I need to validate an Irish phone number but I don't want to make it too user unfriendly, many people are used to writing there phone number with brackets wrapping their area code followed by 5 to 7 digits for their number, some add spaces between the area code or mobile operator.

The format of Irish landline numbers is an area code of between 1 and 4 digits and a number of between 5 to 8 digits.

e.g.

(021) 9876543  
(01)9876543  
01 9876543  
(0402)39385

I'm looking for a regular expression for Javascript/PHP.

I need to validate an Irish phone number but I don't want to make it too user unfriendly, many people are used to writing there phone number with brackets wrapping their area code followed by 5 to 7 digits for their number, some add spaces between the area code or mobile operator.

The format of Irish landline numbers is an area code of between 1 and 4 digits and a number of between 5 to 8 digits.

e.g.

(021) 9876543  
(01)9876543  
01 9876543  
(0402)39385

I'm looking for a regular expression for Javascript/PHP.

Share Improve this question edited Oct 10, 2017 at 10:10 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Jun 2, 2010 at 11:58 Herbage OnionHerbage Onion 1814 silver badges15 bronze badges 2
  • 1 "an area code of between 1 and 4 digits". No, the shortest area codes are two digits. Also, all area codes start with 0, except for special codes, which start with 1. (1800 freephone, 1890 local call, 1580 expensive, etc.) – TRiG Commented Jun 2, 2010 at 12:05
  • TRiG, technically, you're right. But with Dublin (01), some people are used to writing simply (1) the same as Cork people write (21) usually because they've added the country code in first +353 1 1234567 I don't want to confuse these people any further than I need to. Cheers. – Herbage Onion Commented Jun 2, 2010 at 12:21
Add a ment  | 

4 Answers 4

Reset to default 2

It may be worth taking a look at the Validate_IE class - it contains a phoneNumber function which handles a variety of possible Irish phone numbers (with/without area codes, mobile numbers, mobile shortcodes, etc).

Also, if you're working with Irish data, it has a series of additional methods that may one day e in handy for validating everything from Irish bank accounts to driving licence numbers, well worth a look!

This might work. It should account for appearances of spaces anywhere in phone number.

preg_match('|^\s*\(?\s*\d{1,4}\s*\)?\s*[\d\s]{5,10}\s*$|', $phone);

People sometimes split phone number part into 2 parts by spaces.

Update: if you wish to validate phone number and trim spaces at the same time, you can do it like this:

if (preg_match('|^\s*(\(?\s*\d{1,4}\s*\)?\s*[\d\s]{5,10})\s*$|', $phone, $m))
{
    echo $m[1];
}

If it were me, I'd strip spaces and brackets, then verify it's between the minimum and maximum length.

Try this one:

http://regexlib./REDetails.aspx?regexp_id=2485

Modified version excluding the area code:

\d{3}\s\d{4}
发布评论

评论列表(0)

  1. 暂无评论