I was just wondering if you lovely folk would be able to give me some pointers as to where I may be going wrong.
I have implemented a regex checker for UK landline phone numbers and it all seems to work except one set of number. This is my first time at using regular expressions.
Below is the regex that I am using for it:
((01([2-69][1]\s?\d{3}\s?\d{4}$|[2-9][02-9][0-9]\s?\d{3}\s?\d{3}$)))|((02((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4})|([3489]{1}\d{2}\s?\d{3})\s?\d{3})))
The group that is giving me trouble is
((02(((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4})$)... (in the interests of brevity, I have cut out the remaining portion of it. The parentheses are all present and correct in the full regex
I have checked it against regexpal and it seems to validate properly.
I used a test number of
02031111111 <- Valid
0203 111 1111 <- Valid
020 3111 1111 <- Valid
020311111111 <- Invalid (passes validation)
0203 11111111 <- Invalid (passes validation)
020 3111 11111 <- Invalid (fails validation - which is what I want)
This is my code block where the regex function is performed
function valid_phone(landline, country)
{
var homep = '';
switch (country)
{
case 'England':
homep = /^((01([2-69][1]\s?\d{3}\s?\d{4}$|[2-9][02-9][0-9]\s?\d{3}\s?\d{3}$)))|((02((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4})|([3489]{1}\d{2}\s?\d{3})\s?\d{3})))$/;
break;
case 'USA':
homep = /^((((\(\d{3}\))|(\d{3}(-| )))\d{3}(-| )\d{4})|(\+?\d{2}((-| )\d{1,8}){1,5}))$/;
break
default:
homep = /^((01([2-69][1]\s?\d{3}\s?\d{4}$|[2-9][02-9][0-9]\s?\d{3}\s?\d{3}$)))|((02(((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4}))|([3489]{1}\d{2}\s?\d{3})\s?\d{3}$)))$/;
}
return homep.test(landline);
}
This is where it is called in jQuery
$('#landline').blur(function()
{
$lp = $('#landline').val();
$co = $('#country').val();
if (!valid_phone($lp, $co))
{
$('#error').slideDown();
$('#error').append('Wrong phone number format');
}
else
{
$('#error').slideUp();
$('#error').html('');
}
});
As I say, any pointers would be appreciated. I am sure it is something simple. I have also tried without the $ before the last ) and it is still the same.
Thanks in advance.
\d{4}(meaning "exactly 4 digits") at the end. Am I missing something obvious?