我在SO上尝试了其他问题,但它们似乎没有为我的问题提供解决方案:
我有以下简化的验证功能
function Validate() {
var pattern = new RegExp("([^\d])\d{10}([^\d])");
if (pattern.test(document.getElementById('PersonIdentifier').value)) {
return true;
}
else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经测试过,看看它是否正确检索了它.但它并不完全匹配10位数.我不想要或多或少.只接受10位数,否则返回false.
我无法让它发挥作用.试图以多种方式调整模式,但无法正确.也许问题出在其他地方?
我在C#中取得了以下成功:
Regex pattern = new Regex(@"(?<!\d)\d{10}(?!\d)")
Run Code Online (Sandbox Code Playgroud)
什么是可接受的例子:
0123456789,1478589654,1425366989
不能接受的:
a123456789,123456789a,a12345678a
Bra*_*raj 29
您可以尝试test()返回的功能true/false
var str='0123456789';
console.log(/^\d{10}$/.test(str));
Run Code Online (Sandbox Code Playgroud)
或者与不匹配时String#match()返回的函数null
var str='0123456789';
console.log(str.match(/^\d{10}$/));
Run Code Online (Sandbox Code Playgroud)
注意:只需使用^和$匹配整个字符串.
你可以试试这个:
var str = "0123456789";
var pattern = new RegExp("^[0-9]{10}$");
pattern.test(str);
Run Code Online (Sandbox Code Playgroud)