我希望正则表达式验证给定格式的手机和 phone 号码
I want Regex validate mobile and phone number in given format
我希望用户只能输入给定格式的 mobile/phone 数字。
+1234567891 > 当 + 在 plus 之后使用时,用户不能输入 0 或 00,而且如果在 + 之后使用 +,则只能输入 10 到 14 位数字。
01234567891 > 当用户输入单个 0 时,用户必须在 0 之后输入 10 位数字。
00123456789 > 当用户输入双 00 之后,用户不能在双 00 之后输入第三个 0,并且用户必须在双 00 之后输入 10 到 14 位数字。
我的代码是这样的任何人都可以帮助我吗?
string mob = txtmobile.Text.Trim();
Regex plus = new Regex("^[+]?<!(0|00)[0-9]{10,14}$");
Regex zero = new Regex("^[0]{1}[0-9]{10}$");
Regex zeroes = new Regex("^[0]{2}[^0][0-9]{10,14}$");
if (!plus.IsMatch(mob))
{
if (!zero.IsMatch(mob))
{
if (!zeroes.IsMatch(mob))
{
lblmobile.Text = "*Mobile number must be correct format";
lblmobile.Visible = true;
flg = false;
}
}
}
试试这个:\+0[0-9]{9}|\+0[1-9][0-9]{12}|0[1-9][0-9]{8}|00[1-9][0-9]{9}|00[1-9][0-9]{13}
.
或者这样:\+0\d{9}|\+0[1-9]\d{12}|0[1-9]\d{8}|00[1-9]\d{9}|00[1-9]\d{13}
您的模式的常见问题是它们允许在所有情况下输入零。
让我们以 ^[+]?<!(0|00)[0-9]{10,14}$
为例 - 你告诉引擎这个:
- 匹配“+”号
- 断言立即没有零或双零
- 匹配 10 到 14 位数字包括零
您需要这样更改模式:
- 对于加上规则:
^[+][1-9][0-9]{9,13}$
;这转化为:"match plus sign, a digit between 1 and 9 after and 9 to 13 digits between 0 and 9"
- 对于 零 规则:
^0[1-9][0-9]{9}$
- 对于 零 规则:
^00[1-9][0-9]{9,13}$
顺便说一句,zeroes 规则的示例无效; 00
.
后面只有9位数字
我希望用户只能输入给定格式的 mobile/phone 数字。
+1234567891 > 当 + 在 plus 之后使用时,用户不能输入 0 或 00,而且如果在 + 之后使用 +,则只能输入 10 到 14 位数字。
01234567891 > 当用户输入单个 0 时,用户必须在 0 之后输入 10 位数字。
00123456789 > 当用户输入双 00 之后,用户不能在双 00 之后输入第三个 0,并且用户必须在双 00 之后输入 10 到 14 位数字。
我的代码是这样的任何人都可以帮助我吗?
string mob = txtmobile.Text.Trim();
Regex plus = new Regex("^[+]?<!(0|00)[0-9]{10,14}$");
Regex zero = new Regex("^[0]{1}[0-9]{10}$");
Regex zeroes = new Regex("^[0]{2}[^0][0-9]{10,14}$");
if (!plus.IsMatch(mob))
{
if (!zero.IsMatch(mob))
{
if (!zeroes.IsMatch(mob))
{
lblmobile.Text = "*Mobile number must be correct format";
lblmobile.Visible = true;
flg = false;
}
}
}
试试这个:\+0[0-9]{9}|\+0[1-9][0-9]{12}|0[1-9][0-9]{8}|00[1-9][0-9]{9}|00[1-9][0-9]{13}
.
或者这样:\+0\d{9}|\+0[1-9]\d{12}|0[1-9]\d{8}|00[1-9]\d{9}|00[1-9]\d{13}
您的模式的常见问题是它们允许在所有情况下输入零。
让我们以 ^[+]?<!(0|00)[0-9]{10,14}$
为例 - 你告诉引擎这个:
- 匹配“+”号
- 断言立即没有零或双零
- 匹配 10 到 14 位数字包括零
您需要这样更改模式:
- 对于加上规则:
^[+][1-9][0-9]{9,13}$
;这转化为:"match plus sign, a digit between 1 and 9 after and 9 to 13 digits between 0 and 9" - 对于 零 规则:
^0[1-9][0-9]{9}$
- 对于 零 规则:
^00[1-9][0-9]{9,13}$
顺便说一句,zeroes 规则的示例无效; 00
.