Solved

Regular Expression for UK phone number.

Posted on 2006-10-30
2
1,037 Views
Last Modified: 2010-08-13
I am trying to develop a Regular Expression to validate a UK telophone number, with the following criteria:
* The entry must start with a zero.
* Numbers or spaces only.
* Cannot enter less than 10 or more than 15 numbers.
Ie. valid:

01865 215487
01865215487
020 3265 9854

This was my attempt but its not quite right:

      var regex = /^0\d{4}(\s{0,1})\d{6}$/
0
Comment
Question by:jonatec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 250 total points
ID: 17832313
0
 
LVL 30

Expert Comment

by:callrs
ID: 17887420
Thanks.  Code for reference:

<script type="text/javascript">
<!-- Verify UK Phone number -->

var rx1 = /^0\d{4}\s?\d{6}$/
var rx2 = /^0\d{2}\s?\d{4}\s?\d{4}$/
var rx3 = /^0\d{10,15}$/

// test for rx1 and rx2 patterns
var p=new Array("01865 215487","01865215487","020 3265 9854","01234 5678 9012 34","0ab123457890");
var o="";
for (i=0; i<p.length; i++){
      var tested=rx1.test(p[i])
      if(!tested) tested=rx2.test(p[i])
      o=o+p[i]+": "+tested+"\n"
      }
alert(o);

// test for rx3, after trimming out spaces
o="After trimming spaces to allow for 10 to 15 digits\n";
for (i=0; i<p.length; i++){
      var newp=p[i].replace(/\s*/g,"");
      var tested=rx3.test(newp)
      o=o+p[i]+": "+tested+"\n"
      }
alert(o);

</script>
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

752 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