Skip to content

Commit 20295be

Browse files
committed
Added more USA / CAN validation for phone number
This commit closes #2
1 parent 8ced2e8 commit 20295be

File tree

2 files changed

+141
-8
lines changed

2 files changed

+141
-8
lines changed

extra_validations.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,60 @@ func IsValidPhoneNumber(phone string, countryCode string) (success bool, err err
249249
return
250250
}
251251

252-
//todo: finish validation on NPA / NXX
252+
//Break up the phone number into NPA-NXX-XXXX
253+
npa := phone[0:3]
254+
nxx := phone[3:6]
255+
firstDigitOfNpa := npa[0:1]
256+
firstDigitOfNxx := nxx[0:1]
257+
secondThirdDigitOfNxx := nxx[1:3]
258+
259+
//Basic USA/CAN rules can be found: https://en.wikipedia.org/wiki/North_American_Numbering_Plan#Modern_plan
260+
261+
//NPA Cannot start with 1 or 0
262+
if firstDigitOfNpa == "1" || firstDigitOfNpa == "0" {
263+
err = fmt.Errorf("phone number NPA cannot start with " + firstDigitOfNpa)
264+
return
265+
}
266+
267+
//NPA Cannot contain 555 as leading value
268+
if npa == "555" {
269+
err = fmt.Errorf("phone number NPA cannot start with " + npa)
270+
return
271+
}
272+
273+
//NXX Cannot start with 1 or 0
274+
if firstDigitOfNxx == "1" || firstDigitOfNxx == "0" {
275+
err = fmt.Errorf("phone number NXX cannot start with " + firstDigitOfNxx)
276+
return
277+
}
278+
279+
//NXX cannot be N11
280+
if secondThirdDigitOfNxx == "11" {
281+
err = fmt.Errorf("phone number NXX cannot be X" + secondThirdDigitOfNxx)
282+
return
283+
}
253284

254285
case "52": //Mexico
255286

287+
//Rules found so far: https://en.wikipedia.org/wiki/Telephone_numbers_in_Mexico
288+
289+
//Break up the phone number into NPA-NXX-XXXX
290+
npa := phone[0:3]
291+
firstDigitOfNpa := npa[0:1]
292+
256293
//Validate the proper length
257-
if len(phone) != 10 {
258-
err = fmt.Errorf("phone number must be ten digits")
294+
if len(phone) != 8 && len(phone) != 10 { //2002 mexico had 8 digit numbers and went to 10 digits
295+
err = fmt.Errorf("phone number must be either eight or ten digits")
296+
return
297+
}
298+
299+
//NPA Cannot start with 1 or 0
300+
if firstDigitOfNpa == "1" || firstDigitOfNpa == "0" {
301+
err = fmt.Errorf("phone number NPA cannot start with " + firstDigitOfNpa)
259302
return
260303
}
261304

262-
//todo: validate MX number
305+
//todo: validate MX number following Mexico's phone number system (not sure if there are more requirements) (@mrz)
263306

264307
default:
265308
err = fmt.Errorf("country code %s is not accepted", countryCode)

extra_validations_test.go

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
471471
}
472472

473473
//Phone number missing
474-
countryCode = "+1"
474+
countryCode = "+1" //USA / CAN
475475

476476
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
477477
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
@@ -480,7 +480,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
480480
}
481481

482482
//Phone number not right length (USA)
483-
countryCode = "+1"
483+
countryCode = "+1" //USA / CAN
484484
phone = "555-444-3"
485485

486486
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
@@ -490,17 +490,107 @@ func TestIsValidPhoneNumber(t *testing.T) {
490490
}
491491

492492
//Phone number not right length (MX)
493-
countryCode = "+52"
493+
countryCode = "+52" //Mexico
494494
phone = "555-444-3"
495495

496+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
497+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
498+
} else if err.Error() != "phone number must be either eight or ten digits" {
499+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
500+
}
501+
502+
//Phone number not right length (USA)
503+
countryCode = "+1" //USA / CAN
504+
phone = "234-444-3"
505+
496506
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
497507
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
498508
} else if err.Error() != "phone number must be ten digits" {
499509
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
500510
}
501511

512+
//Phone number cannot start with 1
513+
countryCode = "+1" //USA / CAN
514+
phone = "123-123-1234"
515+
516+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
517+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
518+
} else if err.Error() != "phone number NPA cannot start with 1" {
519+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
520+
}
521+
522+
//Phone number cannot start with 0
523+
countryCode = "+1" //USA / CAN
524+
phone = "023-123-1234"
525+
526+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
527+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
528+
} else if err.Error() != "phone number NPA cannot start with 0" {
529+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
530+
}
531+
532+
//Phone number cannot start with 1
533+
countryCode = "+52" //MX
534+
phone = "123-123-1234"
535+
536+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
537+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
538+
} else if err.Error() != "phone number NPA cannot start with 1" {
539+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
540+
}
541+
542+
//Phone number cannot start with 0
543+
countryCode = "+52" //MX
544+
phone = "023-123-1234"
545+
546+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
547+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
548+
} else if err.Error() != "phone number NPA cannot start with 0" {
549+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
550+
}
551+
552+
//Phone number cannot start with 555
553+
countryCode = "+1" //USA / CAN
554+
phone = "555-123-1234"
555+
556+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
557+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
558+
} else if err.Error() != "phone number NPA cannot start with 555" {
559+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
560+
}
561+
562+
//Phone number NXX cannot start with 1
563+
countryCode = "+1" //USA / CAN
564+
phone = "234-123-1234"
565+
566+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
567+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
568+
} else if err.Error() != "phone number NXX cannot start with 1" {
569+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
570+
}
571+
572+
//Phone number NXX cannot start with 0
573+
countryCode = "+1" //USA / CAN
574+
phone = "234-023-1234"
575+
576+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
577+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
578+
} else if err.Error() != "phone number NXX cannot start with 0" {
579+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
580+
}
581+
582+
//Phone number NXX cannot be X11
583+
countryCode = "+1" //USA / CAN
584+
phone = "234-911-1234"
585+
586+
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
587+
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
588+
} else if err.Error() != "phone number NXX cannot be X11" {
589+
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
590+
}
591+
502592
//Phone number valid
503-
countryCode = "+1"
593+
countryCode = "+1" //USA / CAN
504594
phone = "234-234-2345"
505595

506596
if ok, err = IsValidPhoneNumber(phone, countryCode); !ok {

0 commit comments

Comments
 (0)