Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
0779ed68
Commit
0779ed68
authored
8 months ago
by
James Page
Browse files
Options
Downloads
Patches
Plain Diff
Add phone number validation function
parent
b8305c09
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
utils/utils.go
+24
-0
24 additions, 0 deletions
utils/utils.go
utils/utils_test.go
+70
-0
70 additions, 0 deletions
utils/utils_test.go
with
94 additions
and
0 deletions
utils/utils.go
+
24
−
0
View file @
0779ed68
...
@@ -212,6 +212,30 @@ func FormatPhoneNumber(phoneNumber string) string {
...
@@ -212,6 +212,30 @@ func FormatPhoneNumber(phoneNumber string) string {
return
phoneNumber
return
phoneNumber
}
}
func
ValidatePhoneNumber
(
phoneNumber
string
)
(
string
,
error
)
{
phoneNumber
=
string_utils
.
RemoveAllWhiteSpaces
(
phoneNumber
)
for
_
,
char
:=
range
string_utils
.
NonOfficialWhitespaceChars
{
phoneNumber
=
strings
.
ReplaceAll
(
phoneNumber
,
char
,
""
)
}
if
phoneNumber
==
""
{
return
""
,
errors
.
Error
(
"phone number is empty"
)
}
if
!
isPhoneNumber
(
phoneNumber
)
{
return
""
,
errors
.
Error
(
"phone number is invalid"
)
}
return
phoneNumber
,
nil
}
// isPhoneNumber checks if a string is a valid phone number. It only allows numbers and a leading "+". Remove whitespace before calling this function. Note: it does not validate the length of the phone number.
func
isPhoneNumber
(
phoneNumber
string
)
bool
{
phoneRegex
:=
regexp
.
MustCompile
(
`^\+?[0-9]+$`
)
return
phoneRegex
.
MatchString
(
phoneNumber
)
}
func
insertInto
(
s
string
,
index
int
,
characters
string
)
string
{
func
insertInto
(
s
string
,
index
int
,
characters
string
)
string
{
return
s
[
:
index
]
+
characters
+
s
[
index
:
]
return
s
[
:
index
]
+
characters
+
s
[
index
:
]
}
}
...
...
This diff is collapsed.
Click to expand it.
utils/utils_test.go
+
70
−
0
View file @
0779ed68
...
@@ -146,3 +146,73 @@ func TestValidateIPAddress(t *testing.T) {
...
@@ -146,3 +146,73 @@ func TestValidateIPAddress(t *testing.T) {
})
})
}
}
}
}
func
TestValidatePhoneNumber
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
phoneNumber
string
want
string
wantErr
error
}{
{
name
:
"Valid phone number with country code"
,
phoneNumber
:
"+27123456789"
,
want
:
"+27123456789"
,
wantErr
:
nil
,
},
{
name
:
"Valid phone number with country code and spaces"
,
phoneNumber
:
" + 271 2345 6789"
,
want
:
"+27123456789"
,
wantErr
:
nil
,
},
{
name
:
"Valid phone number without country code"
,
phoneNumber
:
"0123456789"
,
want
:
"0123456789"
,
wantErr
:
nil
,
},
{
name
:
"Phone number with spaces"
,
phoneNumber
:
"012 345 6789"
,
want
:
"0123456789"
,
wantErr
:
nil
,
},
{
name
:
"Phone number with non-official whitespaces"
,
phoneNumber
:
"012
\u00A0
345
\u00A0
6789"
,
want
:
"0123456789"
,
wantErr
:
nil
,
},
{
name
:
"Empty phone number"
,
phoneNumber
:
""
,
want
:
""
,
wantErr
:
errors
.
Error
(
"phone number is empty"
),
},
{
name
:
"Invalid phone number with letters"
,
phoneNumber
:
"01234abcde"
,
want
:
""
,
wantErr
:
errors
.
Error
(
"phone number is invalid"
),
},
{
name
:
"Phone number with special characters"
,
phoneNumber
:
"01234!@#$%"
,
want
:
""
,
wantErr
:
errors
.
Error
(
"phone number is invalid"
),
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
ValidatePhoneNumber
(
tt
.
phoneNumber
)
if
got
!=
tt
.
want
{
t
.
Errorf
(
"ValidatePhoneNumber() got = %v, want %v"
,
got
,
tt
.
want
)
}
if
err
!=
nil
&&
err
.
Error
()
!=
tt
.
wantErr
.
Error
()
{
t
.
Errorf
(
"ValidatePhoneNumber() err = %v, want %v"
,
err
,
tt
.
wantErr
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment