1

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.

3
  • Can you provide more examples of valid and invalid input? I think I've found the problem but I'm not sure what is the intended behavior. Commented Jan 29, 2014 at 0:36
  • Well the unexpected failure test case has five 1 at the end, but the regex says \d{4} (meaning "exactly 4 digits") at the end. Am I missing something obvious? Commented Jan 29, 2014 at 0:47
  • @KrzysztofKosiński it's exactly like bishop says. I need to have it with exactly 4 numbers at the end. Commented Jan 29, 2014 at 1:08

1 Answer 1

1

As far as I can understand, the spaces are allowed only in some places in the number, but the number of digits is fixed. In that case, the problem is here:

(\d{3}|\d{4})

This matches either 3 or 4 digits, so it can accept an overlong number. If you want to allow a space after either 3 or 4 numbers, you need to write it like this:

(\d{3}\s\d{4}|\d{4}\s\d{3}|\d{7})

Complete sub-regex for this type of number:

020\s?[378]\s?(\d{3}\s\d{4}|\d{4}\s\d{3}|\d{7})
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for taking the time to answer. When I try it like this, the valid entries become invalid and the invalid ones fail validation as I wish them too. I am stumped on this one
I fixed a typo in my answer (there was d\ instead of \d). It should work correctly now. Note that it will accept a number of the form 020 3 111 1111.
Hi Krzysztof, I did notice that it was d\ and not \d. Thanks for updating the answer though :) However, I am still no further forward with this. I don't understand how the validation is as it should be in the regex checker. Just weird that it doesn't validate properly on the page. I had it like (\d{3}|\d{4})\s?\d{4}) because the phone number needs to be 02033333333 or 0203 333 3333, or 0203 3333333, or 020 33333333 or 020 3333 3333 - just for convention on UK phone numbers. Thanks for looking into it though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.