Skip to content

Additional prefix in UK. #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions additional-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
jQuery.validator.addMethod('mobileUK', function(phone_number, element) {
phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)7(?:[45789]\d{2}|624)\s?\d{3}\s?\d{3})$/);
phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)7(?:[1345789]\d{2}|624)\s?\d{3}\s?\d{3})$/);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[1345789] could also be expressed as [^26\D], but I don't know if that would hurt readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually try to avoid negatives, for that very reason.

Additionally, [345] could be expressed as [3-5] and [789] could be expressed as [7-9] but it is the same number of characters.

In a few years time, when 072xx xxxxxx numbers come into use, it can be simplified to 7([1-5789]\d{2}|624)\d{6} and by then it may be that the rest of 76xx beyond 7624 has been opened up too allowing the much simplified 7[1-9]\d{8} instead.

}, 'Please specify a valid mobile number');

//Matches UK landline + mobile, accepting only 01-3 for landline or 07 for mobile to exclude many premium numbers
jQuery.validator.addMethod('phonesUK', function(phone_number, element) {
phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)(?:1\d{8,9}|[23]\d{9}|7(?:[45789]\d{8}|624\d{6})))$/);
phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)(?:1\d{8,9}|[23]\d{9}|7(?:[1345789]\d{8}|624\d{6})))$/);
}, 'Please specify a valid uk phone number');
// On the above three UK functions, do the following server side processing:
// Compare original input with this RegEx pattern:
Expand Down
39 changes: 34 additions & 5 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,25 +546,54 @@ module("additional methods");

test("phone (us)", function() {
var method = methodTest("phoneUS");
ok( method( "1(212)-999-2345" ), "Valid us phone number" );
ok( method( "212 999 2344" ), "Valid us phone number" );
ok( method( "212-999-0983" ), "Valid us phone number" );
ok(!method( "111-123-5434" ), "Invalid us phone number" );
ok(!method( "212 123 4567" ), "Invalid us phone number" );
ok( method( "1(212)-999-2345" ), "Valid US phone number" );
ok( method( "212 999 2344" ), "Valid US phone number" );
ok( method( "212-999-0983" ), "Valid US phone number" );
ok(!method( "111-123-5434" ), "Invalid US phone number" );
ok(!method( "212 123 4567" ), "Invalid US phone number" );
});

test("phoneUK", function() {
var method = methodTest("phoneUK");
ok( method( "0117 333 5555" ), "Valid UK Phone Number" );
ok( method( "0121 555 5555" ), "Valid UK Phone Number" );
ok( method( "01633 555555" ), "Valid UK Phone Number" );
ok( method( "01298 28555" ), "Valid UK Phone Number" );
ok( method( "015395 55555" ), "Valid UK Phone Number" );
ok( method( "016977 3999" ), "Valid UK Phone Number" );
ok( method( "020 3000 5555" ), "Valid UK Phone Number" );
ok( method( "024 7500 5555" ), "Valid UK Phone Number" );
ok( method( "0333 555 5555" ), "Valid UK Phone Number" );
ok( method( "0500 555555" ), "Valid UK Phone Number" );
ok( method( "055 3555 5555" ), "Valid UK Phone Number" );
ok( method( "07122 555555" ), "Valid UK Phone Number" );
ok( method( "07222 555555" ), "Valid UK Phone Number" );
ok( method( "07322 555555" ), "Valid UK Phone Number" );
ok( method( "0800 555 5555" ), "Valid UK Phone Number" );
ok( method( "0800 355555" ), "Valid UK Phone Number" );
ok( method( "0843 555 5555" ), "Valid UK Phone Number" );
ok( method( "0872 555 5555" ), "Valid UK Phone Number" );
ok( method( "0903 555 5555" ), "Valid UK Phone Number" );
ok( method( "0983 555 5555" ), "Valid UK Phone Number" );
ok( method( "(07122) 555555" ), "Valid UK Phone Number" );
ok( method( "(07222) 555555" ), "Valid UK Phone Number" );
ok( method( "(07322) 555555" ), "Valid UK Phone Number" );
ok( method( "+44 7122 555 555" ), "Valid UK Phone Number" );
ok( method( "+44 7222 555 555" ), "Valid UK Phone Number" );
ok( method( "+44 7322 555 555" ), "Valid UK Phone Number" );
ok(!method( "7222 555555" ), "Invalid UK Phone Number" );
ok(!method( "+44 07222 555555" ), "Invalid UK Phone Number" );
});

test("mobileUK", function() {
var method = methodTest("mobileUK");
ok( method( "07134234323" ), "Valid UK Mobile Number" );
ok( method( "07334234323" ), "Valid UK Mobile Number" );
ok( method( "07624234323" ), "Valid UK Mobile Number" );
ok( method( "07734234323" ), "Valid UK Mobile Number" );
ok( method( "+447134234323" ), "Valid UK Mobile Number" );
ok( method( "+447334234323" ), "Valid UK Mobile Number" );
ok( method( "+447624234323" ), "Valid UK Mobile Number" );
ok( method( "+447734234323" ), "Valid UK Mobile Number" );
ok(!method( "07034234323" ), "Invalid UK Mobile Number" );
ok(!method( "0753423432" ), "Invalid UK Mobile Number" );
Expand Down