Repository: validatorjs/validator.js
Pull request #2109 (2022-12-02 22:02:00)

fix(isMobilePhone): Fix wrong dv-MV mobile phone matching (issue #2101)
Hello,

As reported in #2101, `isMobilePhone`'s dv-MV locale is not matching actually correct mobile phone numbers currently.

See my comments there, which show that that the original RegExp was wrong since it was originally committed, last October:
https://github.com/validatorjs/validator.js/issues/2101#issuecomment-1335802313

The new RegExp is now based on the official ["numbering plan" document from the Maldives Gov ](https://web.archive.org/web/20220614004138/https://cam.gov.mv/docs/Numbering_plan.pdf)
(I've used the archived wayback machine's link, just in case the "live" document above changes in the future, as they seem to be updating that document regularly. [Live version can be found here](https://www.cam.gov.mv/docs/Numbering_plan.pdf), dated June 2022, as of today).

In there they describe the following ranges as valid mobile phone ranges:
**72**X XXXX to **79**X XXXX 
**91**X XXXX to **99**X XXXX 


Further examples can also be found in the comment above.

This PR fixes https://github.com/validatorjs/validator.js/issues/2101
I also updated the tests accordingly, as the previous tests of course are not valid either anymore.

Potential room for improvement here (but I feel like that maybe should be a separate PR as it is more a feature maybe?):
Also account for "local formatting" of the number - i.e. it appears to be common to use a space or hyphen, separating the (non international) digits into two blocks, e.g.

* 722 1234
* 723-4567

I'll create a separate PR for this, after this fix has been pulled in

Thank you


## Checklist

- [x] PR contains only changes related; no stray files, etc.
- [ ] ~~README updated (where applicable)~~
- [x] Tests written (where applicable)


codecov[bot]: # [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/2109?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) Report
Base: **100.00**% // Head: **100.00**% // No change to project coverage :thumbsup:
> Coverage data is based on head [(`c97c42c`)](https://codecov.io/gh/validatorjs/validator.js/pull/2109?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) compared to base [(`531dc7f`)](https://codecov.io/gh/validatorjs/validator.js/commit/531dc7f1f75613bec75c6d888b46480455e78dc7?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).
> Patch has no changes to coverable lines.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##            master     #2109   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          104       104           
  Lines         2308      2308           
  Branches       578       578           
=========================================
  Hits          2308      2308           
```


| [Impacted Files](https://codecov.io/gh/validatorjs/validator.js/pull/2109?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) | Coverage Δ | |
|---|---|---|
| [src/lib/isMobilePhone.js](https://codecov.io/gh/validatorjs/validator.js/pull/2109/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs#diff-c3JjL2xpYi9pc01vYmlsZVBob25lLmpz) | `100.00% <ø> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/2109?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).


Patch:
diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js
index a5424ff..a48fdce 100644
--- a/src/lib/isMobilePhone.js
+++ b/src/lib/isMobilePhone.js
@@ -30,7 +30,7 @@ const phones = {
   'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/,
   'de-CH': /^(\+41|0)([1-9])\d{1,9}$/,
   'de-LU': /^(\+352)?((6\d1)\d{6})$/,
-  'dv-MV': /^(\+?960)?(7[2-9]|91|9[3-9])\d{7}$/,
+  'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/,
   'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/,
   'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/,
   'en-AI': /^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/,


Test patch:
diff --git a/test/validators.test.js b/test/validators.test.js
index 0405a7d..9d98083 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -9495,18 +9495,30 @@ describe('Validators', () => {
       {
         locale: 'dv-MV',
         valid: [
-          '+960973256874',
-          '781246378',
-          '+960766354789',
-          '+960912354789',
+          '+9609112345',
+          '+9609958973',
+          '+9607258963',
+          '+9607958463',
+          '9609112345',
+          '9609958973',
+          '9607212963',
+          '9607986963',
+          '9112345',
+          '9958973',
+          '7258963',
+          '7958963',
         ],
         invalid: [
           '+96059234567',
           '+96045789',
           '7812463784',
-          '+960706985478',
-          '+960926985478',
           'NotANumber',
+          '+9607112345',
+          '+9609012345',
+          '+609012345',
+          '+96071123456',
+          '3412345',
+          '9603412345',
         ],
       },
       {