Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2321
  • Last Modified:

Dutch Phonenumber Validator (Regular Expressions) - Quick answer required

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
pizzametsalami
Asked:
pizzametsalami
  • 9
  • 9
  • 8
  • +4
6 Solutions
 
pizzametsalamiAuthor Commented:
Oh and I would allmost forget:

I would like havin a regexp for validating e-mailaddresses --> any idea's? thnx
0
 
venkateshwarrCommented:
</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
 
searlasCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

tnx in advance
0
 
venkateshwarrCommented:
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
 
alambresCommented:
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
 
ZvonkoSystems architectCommented:
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
 
pizzametsalamiAuthor Commented:
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
 
pizzametsalamiAuthor Commented:
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
 
jaysolomonCommented:
that email validation allows _______@_____.com is this what you want?
0
 
pizzametsalamiAuthor Commented:
eeuhm -- i'll check on that -- tnx :D
0
 
jaysolomonCommented:
actually it allows

______@______.___
0
 
jaysolomonCommented:
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
 
pizzametsalamiAuthor Commented:
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
 
jaysolomonCommented:
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
 
jaysolomonCommented:
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
 
jaysolomonCommented:
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
 
searlasCommented:
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
 
jaysolomonCommented:
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
 
searlasCommented:
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
 
jaysolomonCommented:
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
 
ZvonkoSystems architectCommented:
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
 
searlasCommented:
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
 
jaysolomonCommented:
>>>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
 
pizzametsalamiAuthor Commented:
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
 
searlasCommented:
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
 
searlasCommented:
-- 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
 
searlasCommented:
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
 
ren_bCommented:
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
 
pizzametsalamiAuthor Commented:
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
 
searlasCommented:
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
 
ZvonkoSystems architectCommented:
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
 
pizzametsalamiAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 9
  • 8
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now