英文:
How to validate Cameroon phone number using Regex in Flutter
问题
以下是您要翻译的内容:
我正在使用Flutter构建移动应用程序,并且希望在用户输入手机号码时验证并检查该手机号码属于哪家移动运营商(MTN CM还是Orange CM)。
假设我附加国家代码+237,以下是喀麦隆手机号码要有效且属于特定运营商的一般规则:
通用规则:
- 所有手机号码必须以数字6开头。
- 所有手机号码长度为9位。
- 所有手机号码不能包含任何空格或特殊字符/字母。
对于MTN CM
- MTN号码可以以8开头,后面可以是任何数字(688684749)。
- MTN号码可以以7开头,后面可以是任何数字(678684749)。
- 此外,MTN号码可以以5开头,后跟一个范围(0 - 4),然后可以是任何数字(651684749,652684749,653684749,654684749)。
对于Orange CM
- Orange号码可以以9开头,后面可以是任何数字(698684749)。
- 此外,Orange号码可以以5开头,后跟一个范围(5 - 9),然后可以是任何数字(655684749,656684749,657684749,658684749)。
尝试
这是我目前在截图上所做的,但它不像预期的那样工作,我已经阅读了正则表达式文档,但似乎让我感到不知所措。
期望
我希望在手机号码匹配时在TextFormField的后缀处显示移动运营商的标志,并阻止用户提交请求,如果手机号码不匹配的话。
英文:
I am building a mobile application in Flutter and I want to validate and check which phone number belongs to which mobile operator(MTN CM or Orange CM) when the user inputs a phone number.
Assuming that I append the country code +237, these are the general rules for a number to be valid and belongs to a particular operator in Cameroon:-
General Rule:
- All mobile numbers MUST begin with a digit 6.
- All mobile numbers have a length of 9 digits.
- All mobile numbers MUST NOT have any space or special character/letter.
For MTN CM
- An MTN number can start with 8 and take up any digit till the end (688684749).
- An MTN number can start with 7 and take up any digit till the end (678684749).
- Also an MTN number can start with 5 followed by a range (0 - 4) and take up any digit till the end (651684749, 652684749, 653684749, 654684749).
For Orange CM
- An Orange number can start with 9 and take up any digit till the end (698684749).
- Also an Orange number can start with 5 followed by a range (5 - 9) and take up any digit till the end (655684749, 656684749, 657684749, 658684749).
RegExp regexMtn = RegExp(r'(^6+(?:\+?8|7|5[1-4])+[0-9]{9}$)');
Attempt
That is what I have done so far on the screenshot but it doesn't work as expected, I have gone through the regex documentation but it seems to overwhelm me.
Expectation
I expect to display the mobile operator's logo at the suffix of the TextFormField when it matches to the phone number and prevent the user from submitting the request if the phone number doesn't matches.
答案1
得分: 2
验证号码:
```none
^ # 匹配字符串开头
6 # 以'6'开头
\d{8} # 然后是8位数字
$ # 紧接字符串末尾。
检查是否为 MTN 号码:
^ # 匹配字符串开头
6 # 以'6'开头
(?: # 然后是
[87]\d{7} # '8' 或 '7',然后是其他7位数字
| # 或者
5[0-4]\d{6} # '5',0到4之间的任意数字,然后是其他6位数字
) # 紧接字符串末尾
$ # 。
检查是否为 Orange 号码(解释基本相同):
^6(?:9\d{7}|5[5-9]\d{6})$
<details>
<summary>英文:</summary>
To validate the number:
```none
^ # Match at the beginning of the string
6 # something that starts with '6'
\d{8} # and followed by 8 more digits
$ # right before the end of the string.
Try it on regex101.com.
To check if it's a MTN number:
^ # Match at the beginning of the string
6 # something that starts with '6'
(?: # followed by either
[87]\d{7} # '8' or '7', then 7 other digits
| # or
5[0-4]\d{6} # '5', any digit from 0 to 4, then 6 other digits
) # right before
$ # the end of the string.
Try it on regex101.com.
To check if it's an Orange number (the explanation is mostly the same):
^6(?:9\d{7}|5[5-9]\d{6})$
Try it on regex101.com.
答案2
得分: 0
这是一个用于验证Orange和MTN号码的函数,根据提供的标准进行验证。
/**
* 检查一个电话号码是否为MTN或Orange号码
* @param phoneNumber 要测试的电话号码
* @returns 如果是MTN则返回0,如果是Orange则返回1,否则返回-1。
*/
export function validatePhoneNumber(phoneNumber: string) {
const mtnRegexp = new RegExp(/^6(((7|8)[0-9]{7}$)|(5[0-4][0-9]{6}$))/);
const orangeRegexp = new RegExp(/^6(((9)[0-9]{7}$)|(5[5-9][0-9]{6}$))/);
if (mtnRegexp.test(phoneNumber)) return 0;
else if (orangeRegexp.test(phoneNumber)) return 1;
return -1;
}
当结合这两个正则表达式时,我们得到:
const regExp = new RegExp(/^6(5|7|8|9)[0-9]{7}$/);
英文:
Here is a function that validates an Orange and MTN number with the provided criterial.
/**
* Check if a phone number is an MTN or Orange number
* @param phoneNumber phone number to test
* @returns 0 if MTN, 1 if Orange, and -1 otherwise.
*/
export function validatePhoneNumber(phoneNumber: string) {
const mtnRegexp = new RegExp(/^6(((7|8)[0-9]{7}$)|(5[0-4][0-9]{6}$))/);
const orangeRegexp = new RegExp(/^6(((9)[0-9]{7}$)|(5[5-9][0-9]{6}$))/);
if (mtnRegexp.test(phoneNumber)) return 0;
else if (orangeRegexp.test(phoneNumber)) return 1;
return -1;
}
When combining both regular expressions we get:
const regExp = new RegExp(/^6(5|7|8|9)[0-9]{7}$/);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论