Skip to content

Commit 5a5c153

Browse files
committed
add phone number validation check
1 parent 7db3227 commit 5a5c153

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: go
22
go:
3-
- 1.4.x
43
- 1.11.x
54
- 1.16.x
65
- 1.x

validate/nationalid/nationalid.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ func IsValid(value string) (bool, error) {
2929
valueSlices = append(valueSlices, uint8(i))
3030
}
3131

32-
// check #1 > *position
33-
s, err := calculateNationalIDNumbers(&valueSlices)
34-
if err != nil {
35-
return true, err
36-
}
32+
s := calculateNationalIDNumbers(&valueSlices)
3733

3834
// check #2
3935
s %= 11
@@ -46,10 +42,10 @@ func IsValid(value string) (bool, error) {
4642
}
4743

4844
//calculateNationalIDNumbers Yo
49-
func calculateNationalIDNumbers(valueSlices *[]uint8) (sum int, err error) {
45+
func calculateNationalIDNumbers(valueSlices *[]uint8) (sum int) {
5046
var i uint8
5147
for i = 10; i >= 2; i-- {
5248
sum += int(i * (*valueSlices)[10-i])
5349
}
54-
return sum, nil
50+
return sum
5551
}

validate/phone/phone.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package phone
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
)
7+
8+
//IsValid mobile phone number validation
9+
func IsValid(value string) (bool, error) {
10+
re := regexp.MustCompile(`^(?:0|\+98|0098)(\d{10})$`)
11+
result := re.FindStringSubmatch(value)
12+
if len(result) > 0 {
13+
return true, nil
14+
}
15+
return false, errors.New("invalid phone number")
16+
}

validate/phone/phone_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package phone
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPhone(t *testing.T) {
8+
tables := []struct {
9+
value string
10+
ok bool
11+
}{
12+
{value: "2130396217", ok: false},
13+
{value: "+9893909200111", ok: false},
14+
{value: "0098939092011", ok: false},
15+
{value: "09390920011", ok: true},
16+
{value: "+989390920011", ok: true},
17+
{value: "00989390920011", ok: true},
18+
}
19+
for _, data := range tables {
20+
ok, err := IsValid(data.value)
21+
if ok != data.ok {
22+
t.Errorf("PhoneNumber(%q) = %v, ok %v,err %s", data.value, ok, data.ok, err)
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)