Question

Dutch Phonenumber Validator (Regular Expressions) - Quick answer required

Asked by: pizzametsalami

Heeey,

It's been a while since i've been here; missed me??? ;)

I am working on a database project for a traveling agent. For that project I am creating a form where e-mailaddresses and phonenumbers can be entered.
As far as I know, phonenumbers can be entered in following forms:

-      0599-123456
-      050-1234567
-      06-12345678

and of course; to make international numbers valid, it must be possible to add +31 or 0031 to the front of the number:
-      +31599-123456
-      003150-1234567

I would like a routine to validate these kind of numbers, and preferably convert these numbers to one format. Can anyone help me creating this using regular expressions or so?

I would like to have a quick answer, so i can go on with my project, and any comment is apreciated.

Greetz,
PizzaMetSalami

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-03-24 at 02:20:15ID20930059
Tags

dutch

,

number

,

regular

,

expression

Topic

JavaScript

Participating Experts
6
Points
500
Comments
33

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Format PhoneNumber function
    Does anyone know of a function (like FormatCurrency or FormatNumber) that will format a number to display as a phone number (i.e. (xxx) xxx-xxxx). I've accomplished this with LEFT, MID and RIGHT functions but it's a real pain if the number being formatted is not always 7 or ...
  2. EditMask for all international and locale phonenumbers
    Hi, Does anybody know how to create an editmask for all international and locale phonenumbers? Basically I need information on: 1. What is the shortest and longest netnumber (area)? 2. Are there any netnumber (area) that do not start with a zero? 3. Are there countries where...
  3. Adding PhoneNumber via DSADD
    I get an error message when I try to add a phonenumber to a users profile. Error: Object does not support this proberty or method: 'tel'. Code:800A01B6 According to microsoft and others 'tel' is the right proberty to use when you want to get/write to the PhoneNumber field....

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: pizzametsalamiPosted on 2004-03-24 at 02:21:15ID: 10665837

Oh and I would allmost forget:

I would like havin a regexp for validating e-mailaddresses --> any idea's? thnx

 

by: venkateshwarrPosted on 2004-03-24 at 03:07:52ID: 10666319

</script>

function checkLocal(phoneNo) {
 var phoneRE = /^\d{2,4}-\d{6,8}/;
 if (phoneNo.match(phoneRE)) {
   return true;
 } else {
   alert("The phone number entered is invalid!");
   return false;
 }
}

function checkInternational(phoneNo) {
 var phoneRE1 = /^\+31\d{3}-\d{6}/;
 var phoneRE2 = /0031\d{2}-\d{7}/;
 if (phoneNo.match(phoneRE1)||phoneNo.match(phoneRE2)) {
   return true;
 } else {
   alert("The phone number entered is invalid!");
   return false;
 }
}

//checkPhoneNumber("(123) 345-7890");
checkLocal("0599-123456");
checkLocal("050-1234567");
checkLocal("06-12345678");

checkInternational("+31599-123456");
checkInternational("003150-1234567");

</script>

 

by: searlasPosted on 2004-03-24 at 03:10:08ID: 10666350

venkateshwarr - yours accepts:
06-123456
0599-12345678
I don't think these are supposed to be acceptable...

<html>
    <head>
        <script type="text/javascript">
        function changePhone(field) {
            // validate phone number and convert to fixed format...
            // International dutch phone number: (+|00)31\d{9}
            // intranational dutch phone number: 0\d{9}
            var value = field.value;
            var phoneEx = /^((\+|00)31|0)(\d{9})$/;
            // remove all spaces and dashes
            value = value.replace(/\s|-/g, "");
            // check if it's valid phone number...
            if (phoneEx.test(value)) {
                // reformat into standard format
                var matches =  value.match(phoneEx);
                // set field to standard format
                field.value = "+31" + matches[3];
            } else {
                alert("invalid phone number");
            }
        }
        </script>
    </head>
    <body>
        <input type="text" onchange="changePhone(this)">
    </body>
</html>

 

by: pizzametsalamiPosted on 2004-03-24 at 03:16:22ID: 10666423

tnx! -- i'll check on that in half an hour -- havin a break now ;)

tnx in advance

 

by: venkateshwarrPosted on 2004-03-24 at 03:35:48ID: 10666675

I dont know if there is a better way, but this works..

function checkLocal(phoneNo)
{
var separator = '-';
var stringArray = phoneNo.split(separator);
var pre=stringArray[0];
var len=pre.length;
eval("var phoneRE = /^\\d{"+(len)+",4}-\\d{"+(10-len)+",10}/;");
document.write("|"+phoneRE+"|-"+phoneNo);

 if (phoneNo.match(phoneRE)) {
   document.write("valid! <br/>");
   return true;
 } else {
 document.write("invalid! <br/>");
   //alert("The phone number entered is invalid!");
   return false;
 }
}

venkat.

 

by: alambresPosted on 2004-03-24 at 03:45:29ID: 10666752

mail validation:
<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
      function valMail(obj)
      {
            var re = /^\w+@\w+\.\w+$/;
            return re.test(obj.value);
      }

</script>

</HEAD>
<BODY>
<input width="100px" onblur="if (!valMail(this)){alert('incorrect mail address');this.focus();}">

</BODY>
</HTML>

 

by: ZvonkoPosted on 2004-03-24 at 03:56:11ID: 10666804

Here my version:


<html>
<head>
<script>
function checkForm(theForm){
  email = /^\w+([\.\-]\w+)*\@\w+([\-\.]\w+)*\.[a-z]{2,4}$/i;
  phone = /^(\+[1-9])?(\d+\-?\d+)+$/;
  if(!theForm.EmailAddress.value.match(email)){
    alert("Wrong email: "+theForm.EmailAddress.value);
    theForm.EmailAddress.select();
    theForm.EmailAddress.focus();
    return false;
  }
  if(!theForm.PhoneNumber.value.match(phone)){
    alert("Wrong phone number: "+theForm.PhoneNumber.value);
    theForm.PhoneNumber.select();
    theForm.PhoneNumber.focus();
    return false;
  } else {
    phoneNum = theForm.PhoneNumber.value;
    phoneNum = phoneNum.replace(/^00/,"+").replace(/^0/,"+31").replace(/\-/g, "");
    theForm.PhoneNumber.value = phoneNum;
    return true;
  }
}
</script>
</head>
<body>
<form onSubmit="return checkForm(this)">
Email:
<input type=text name="EmailAddress" size="50"><br>
Phone:
<input type=text name="PhoneNumber" size="50"><br>
<input type=submit>
</form>
</body>
</html>


 

by: pizzametsalamiPosted on 2004-03-24 at 04:57:54ID: 10667159

Having all your comments in mind, I have created the function which I think is capable of doing what I want. This is the code:
function checkNumber(num){
      // Remove dashes and spaces
      num = num.replace(/[-.\s]/gi, "");
      // Replace + with 00
      num = num.replace(/[+]/gi, "00");
      num = num.replace(/\D/gi, "");
      
      if (num.substr(0, 1)!="0"){
            // Invalid number
            return false;
      }
      else{
            if (num.substr(0, 2)=="00"){
                  // International Number
                  if (num.substr(2,2)=="31"){
                        // Dutch numbers don't need 31
                        num = "0" + num.substr(4);
                        return (num.length==10)?num:false;
                  }
                  else{
                        // Others do.
                        return (num.length==13)?num:false;
                  }
            }
            
            
      }
      return num;
}

I hope this is clear -- and I am curious whether or not this function works. Can anyone please try it? thanx.

I am now checking the e-mail address section.

 

by: pizzametsalamiPosted on 2004-03-24 at 05:17:55ID: 10667301

The email address section:
I have taken the function of alambres, and slightly modified it, into this function:

    function valMail(obj)
     {
     
      
          var re = /^\w+(\.\w+)*@\w+(\.\w+)+$/;
         
         
          // Translated:
          //
          // must start with one or more (+) text characters (\w)
          // (can be followed by a . (\.) and one or more text characters (\w) zero or more times (*))
          // then there must be a @sign, followed by one ore more text characters.
          // (can be followed by a . (\.) and one or more text characters (\w) one or more times (+))
         
          return re.test(obj);
     }

Please tell me whether this is the right way to do this.

Greetz, PiZZaMeTSaLaMi

 

by: jaysolomonPosted on 2004-03-24 at 06:21:21ID: 10667904

that email validation allows _______@_____.com is this what you want?

 

by: pizzametsalamiPosted on 2004-03-24 at 06:23:33ID: 10667923

eeuhm -- i'll check on that -- tnx :D

 

by: jaysolomonPosted on 2004-03-24 at 06:26:26ID: 10667952

actually it allows

______@______.___

 

by: jaysolomonPosted on 2004-03-24 at 06:41:09ID: 10668123

Zvonko is alot better at reg exps than i am he may or may not like this one or may be able to convert it so it will be better optimized

var re = /^([a-z0-9]){1,}\w+([\.\-]\w+)*\@([a-z0-9]){1,}([\-\.]\w+)*\.[com|net|org|biz|gov|info|name|sr|us|nu|ws|tv|de]{2,4}$/i;

it insures you that atleast the first character is a-zA-Z0-9 and the last part is actually a domain

 

by: pizzametsalamiPosted on 2004-03-24 at 06:43:42ID: 10668157

New version:

function valMail(obj){
var re = /^([a-zA-Z0-9])+((\.|_)[a-zA-Z0-9]+)*@[a-zA-Z0-9]+((\.|\-)[a-zA-Z0-9]+)*(\.[a-zA-Z0-9]{2,3})$/;
return re.test(obj);
}

 

by: jaysolomonPosted on 2004-03-24 at 06:47:35ID: 10668200

unless your sure your audience is not going to have a domain that is either .info, or .name then its ok but if you are not sure you should change the last part to {2,4}

and here is a correction on the one i submitted
[com|net|org|biz|gov|info|name|sr|us|nu|ws|tv|de]$/i;

 

by: jaysolomonPosted on 2004-03-24 at 06:50:04ID: 10668220

also domains as of now and probally will not end with numbers so

this
(\.[a-zA-Z0-9]{2,3})

should be

(\.[a-zA-Z]{2,3})

 

by: jaysolomonPosted on 2004-03-24 at 06:54:19ID: 10668260

oh here is one zvonko did yesterday or the day before and i just added the ending to it

var re =/^[a-z][a-z0-9]*([\.\-\_][a-z][a-z0-9]*)*\@([a-z][a-z0-9]*\.)*([a-z][a-z0-9]*)\.[com|net|org|biz|gov|info|name|sr|us|nu|ws|tv|de]$/i;

 

by: searlasPosted on 2004-03-24 at 07:47:38ID: 10668763

jaysolomon,
Isn't your [com|net|org|biz|gov|info|name|sr|us|nu|ws|tv|de] a character class?  i.e. it should be
(com|net|org|biz|gov|info|name|sr|us|nu|ws|tv|de)

Otherwise, you're basically saying
[com|netrgbizvfasuwd]

or, alphabetically
[abcdefgimnorstuvwz|]

 

by: jaysolomonPosted on 2004-03-24 at 07:52:21ID: 10668808

your right it was a copy paste error

var re =/^[a-z][a-z0-9]*([\.\-\_][a-z][a-z0-9]*)*\@([a-z][a-z0-9]*\.)*([a-z][a-z0-9]*)\.(com|net|org|biz|gov|info|name|sr|us|nu|ws|tv|de)$/i;

 

by: searlasPosted on 2004-03-24 at 08:01:10ID: 10668869

And you left out uk!

But, more importantly, you left out 'nl' which is going to be regularly used by people with Dutch phone numbers.
And, given national stereotypes, I'd have thought there'd be a few who will soon have email address ending in xxx...

So, maybe it's just safer to allow 2 to 4 alphabetic characters at the end of an email address after all...

 

by: jaysolomonPosted on 2004-03-24 at 08:06:09ID: 10668924

var re =/^[a-z][a-z0-9]*([.-_][a-z][a-z0-9]*)*@([a-z][a-z0-9]*.)*([a-z][a-z0-9]*).([a-z]{2}|com|net|org|biz|gov|info|name)$/i;


This is care of Zvonko

 

by: ZvonkoPosted on 2004-03-24 at 08:18:39ID: 10669034

Sorry jAy, I sent you a wrong line.
Now I see that dot to underscore is marked as range by dash.
It has to be rewritten to this:
var re =/^[a-z][a-z0-9]*([.\-_][a-z][a-z0-9]*)*@([a-z][a-z0-9]*.)*([a-z][a-z0-9]*).([a-z]{2}|com|net|org|biz|gov|info|name)$/i;

 

by: searlasPosted on 2004-03-24 at 08:22:09ID: 10669064

Still too restrictive, elminating 'edu'.  That's a lot of unhappy young folk...
And hey... there's a 'museum' tld too (not that I've ever seen an email address ending in .museum, but why rule it out?)


From http://www.icann.org/tlds/
In the 1980s, seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org) were created. Domain names may be registered in three of these (.com, .net, and .org) without restriction; the other four have limited purposes.

Over the next twelve years, various discussions occurred concerning additional gTLDs, leading to the selection in November 2000 of seven new TLDs for introduction. These were introduced in 2001 and 2002. Four of the new TLDs (.biz, .info, .name, and .pro) are unsponsored. The other three new TLDs (.aero, .coop, and .museum) are sponsored.

 

by: jaysolomonPosted on 2004-03-24 at 08:40:29ID: 10669209

>>>Still too restrictive

That is why you should know your audience

that is priority number 1 in designing

know who your audience will be

 

by: pizzametsalamiPosted on 2004-03-24 at 09:31:11ID: 10669683

Well thanx for the complete discussion here :) I just came home and saw these comments. Just before I reward my points I would like to know what you fellows thought about my comments containing the code I am using at this moment.

Please post the ins and outs of this code, and it's raining points time!

 

by: searlasPosted on 2004-03-24 at 09:57:01ID: 10669931

Dissection of your code:
function checkNumber(num){
     // Remove dashes and spaces
     num = num.replace(/[-.\s]/gi, "");
-- no need for the case-insensitivity
     // Replace + with 00
     num = num.replace(/[+]/gi, "00");
-- no need for character class or case-insensitivity
-- replaces + ANYWHERE in the string... is that valid?
     num = num.replace(/\D/gi, "");
-- ditto
-- they could all be chained together in Zvonko style as:
     num = num.replace(/[-.\s]/g, "").replace(/\+/g, "00").replace(/\D/g, "");
     
     if (num.substr(0, 1)!="0"){
-- this can/will throw a javascript error if the user hasn't entered anything, or, if they entered '--abc.d.e.f---'
          // Invalid number
          return false;
     }
     else{
          if (num.substr(0, 2)=="00"){
-- this can/will throw a javascript error if the user only entered a zero on it's own
               // International Number
               if (num.substr(2,2)=="31"){
-- this can/will throw a javascript error if the user only entered '123'
                    // Dutch numbers don't need 31
                    num = "0" + num.substr(4);
                    return (num.length==10)?num:false;
               }
               else{
                    // Others do.
                    return (num.length==13)?num:false;
               }
          }
         
         
     }
     return num;
}

 

by: searlasPosted on 2004-03-24 at 09:57:34ID: 10669935

-- this can/will throw a javascript error if the user only entered '123'
should have read
-- this can/will throw a javascript error if the user only entered '012'

 

by: searlasPosted on 2004-03-24 at 10:07:59ID: 10670027

Also, if the phone number entered is not international, you (pizzametsalami) just seem to accept it as valid?
e.g. 012345

if ( num.substr(0, 1) != "0" ) {
  // Invalid number
  return false;
} else {
  if ( num.substr(0, 2) == "00" ) {
  ....both paths have return statement...
  }
  // else number did not start with 00, and you don't check anthing about it.. e.g. length...
}
return num;  

 

by: ren_bPosted on 2004-03-24 at 10:49:51ID: 10670435

i contribute this for the phone numbers via regex:

function checkPhone(num){
  return /\s*?(?:(?:0[^0]\d{8})|(?:00[^0]\d{10}))\s*?$/.test(num.replace(/\+/,"00").replace(/\-/,""));
}

as for the email, theres just tpo many possibilities to tie it all to just one "general purpose" regex.  there is a lot of info about email regex's and a few good examples: http://www.experts-exchange.com/Q_20900341.html

 

by: pizzametsalamiPosted on 2004-03-24 at 12:23:26ID: 10671339

This discussion has given me enough base to get myself thinking of a good working example. I will work out all things that have been said, and I'll post my finally used code. Thank you all for competing in this question, and pleaz tell me, who wants the points? ;)

 

by: searlasPosted on 2004-03-25 at 01:12:49ID: 10675320

pizzametsalami,

You can split the points between the people whose comments helped you - you don't need to choose one person to receive all the points.
http://www.experts-exchange.com/help.jsp#hi19

 

by: ZvonkoPosted on 2004-03-25 at 01:56:53ID: 10675513

And perhaps is the better question: who wants to be helped next time when he needs solutions?

You cannot give away points: you do not hold some.
This is a experts exchange board and the points are only a symbolic Thank You, not more and not less. Keep that in mind.
 
I do not need your points.



 

by: pizzametsalamiPosted on 2004-03-25 at 04:04:40ID: 10676133

haha -- tnx -- i've ben here long enough to understand that there's nothing you can do with gained points -- and I know that I can split my points.

I'll finish this question soon.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...