diff --git a/patterns.go b/patterns.go index 1cf9726..e55451c 100644 --- a/patterns.go +++ b/patterns.go @@ -48,6 +48,7 @@ const ( hasUpperCase string = ".*[[:upper:]]" hasWhitespace string = ".*[[:space:]]" hasWhitespaceOnly string = "^[[:space:]]+$" + IMEI string = "^[0-9a-f]{14}$|^\\d{15}$|^\\d{18}$" ) // Used by IsFilePath func @@ -100,4 +101,5 @@ var ( rxHasUpperCase = regexp.MustCompile(hasUpperCase) rxHasWhitespace = regexp.MustCompile(hasWhitespace) rxHasWhitespaceOnly = regexp.MustCompile(hasWhitespaceOnly) + rxIMEI = regexp.MustCompile(IMEI) ) diff --git a/types.go b/types.go index f42a346..b57b666 100644 --- a/types.go +++ b/types.go @@ -162,6 +162,7 @@ var TagMap = map[string]Validator{ "ISO3166Alpha2": IsISO3166Alpha2, "ISO3166Alpha3": IsISO3166Alpha3, "ISO4217": IsISO4217, + "IMEI": IsIMEI, } // ISO3166Entry stores country codes diff --git a/validator.go b/validator.go index 48f2f86..298f992 100644 --- a/validator.go +++ b/validator.go @@ -737,6 +737,11 @@ func IsLongitude(str string) bool { return rxLongitude.MatchString(str) } +// IsIMEI check if a string is valid IMEI +func IsIMEI(str string) bool { + return rxIMEI.MatchString(str) +} + // IsRsaPublicKey check if a string is valid public key with provided length func IsRsaPublicKey(str string, keylen int) bool { bb := bytes.NewBufferString(str) diff --git a/validator_test.go b/validator_test.go index 4fef8ab..631dff0 100644 --- a/validator_test.go +++ b/validator_test.go @@ -4246,3 +4246,21 @@ func TestIsType(t *testing.T) { } } } + +func TestIsIMEI(t *testing.T) { + tests := []struct { + param string + expected bool + }{ + {"990000862471854", true}, + {"351756051523999", true}, + {"9900008624718541", false}, + {"1", false}, + } + for _, test := range tests { + actual := IsIMEI(test.param) + if actual != test.expected { + t.Errorf("Expected IsIMEI(%q) to be %v, got %v", test.param, test.expected, actual) + } + } +}