Solved

Dutch Phonenumber Validator (Regular Expressions) - Quick answer required

Posted on 2004-03-24
33
2,029 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:pizzametsalami
  • 9
  • 9
  • 8
  • +4
33 Comments
 
LVL 3

Author Comment

by:pizzametsalami
ID: 10665837
Oh and I would allmost forget:

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

Assisted Solution

by:venkateshwarr
venkateshwarr earned 84 total points
ID: 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>
0
 
LVL 7

Assisted Solution

by:searlas
searlas earned 164 total points
ID: 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>
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 3

Author Comment

by:pizzametsalami
ID: 10666423
tnx! -- i'll check on that in half an hour -- havin a break now ;)

tnx in advance
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 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.
0
 
LVL 5

Assisted Solution

by:alambres
alambres earned 84 total points
ID: 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>
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 84 total points
ID: 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>


0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 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.
0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 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
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10667904
that email validation allows _______@_____.com is this what you want?
0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 10667923
eeuhm -- i'll check on that -- tnx :D
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10667952
actually it allows

______@______.___
0
 
LVL 16

Accepted Solution

by:
jaysolomon earned 84 total points
ID: 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
0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 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);
}
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 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;
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 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})
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 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;
0
 
LVL 7

Assisted Solution

by:searlas
searlas earned 164 total points
ID: 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|]
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 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;
0
 
LVL 7

Expert Comment

by:searlas
ID: 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...
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 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
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 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;

0
 
LVL 7

Expert Comment

by:searlas
ID: 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.
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 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
0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 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!
0
 
LVL 7

Expert Comment

by:searlas
ID: 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;
}
0
 
LVL 7

Expert Comment

by:searlas
ID: 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'
0
 
LVL 7

Expert Comment

by:searlas
ID: 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;  
0
 
LVL 6

Expert Comment

by:ren_b
ID: 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
0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 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? ;)
0
 
LVL 7

Expert Comment

by:searlas
ID: 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

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 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.



0
 
LVL 3

Author Comment

by:pizzametsalami
ID: 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.

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question