我在一些网站上找到了这个代码,它工作得很完美。它验证电话号码是以下格式之一: (123) 456-7890或123-456-7890
问题是我的客户端(我不知道为什么,可能是客户端)想要添加另一种格式,连续的十个数字,像这样:1234567890。
我用这个正则表达式,
/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/
我如何添加它也验证另一种格式?我不擅长使用正则表达式。
我在一些网站上找到了这个代码,它工作得很完美。它验证电话号码是以下格式之一: (123) 456-7890或123-456-7890
问题是我的客户端(我不知道为什么,可能是客户端)想要添加另一种格式,连续的十个数字,像这样:1234567890。
我用这个正则表达式,
/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/
我如何添加它也验证另一种格式?我不擅长使用正则表达式。
当前回答
如果你使用输入标签,这段代码将帮助你。这段代码是我自己写的,我认为这是很好的输入方式。但是你可以用你的格式来改变它。它将帮助用户纠正输入标签上的格式。
$("#phone").on('input', function() { //this is use for every time input change.
var inputValue = getInputValue(); //get value from input and make it usefull number
var length = inputValue.length; //get lenth of input
if (inputValue < 1000)
{
inputValue = '1('+inputValue;
}else if (inputValue < 1000000)
{
inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, length);
}else if (inputValue < 10000000000)
{
inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, 6) + '-' + inputValue.substring(6, length);
}else
{
inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, 6) + '-' + inputValue.substring(6, 10);
}
$("#phone").val(inputValue); //correct value entered to your input.
inputValue = getInputValue();//get value again, becuase it changed, this one using for changing color of input border
if ((inputValue > 2000000000) && (inputValue < 9999999999))
{
$("#phone").css("border","black solid 1px");//if it is valid phone number than border will be black.
}else
{
$("#phone").css("border","red solid 1px");//if it is invalid phone number than border will be red.
}
});
function getInputValue() {
var inputValue = $("#phone").val().replace(/\D/g,''); //remove all non numeric character
if (inputValue.charAt(0) == 1) // if first character is 1 than remove it.
{
var inputValue = inputValue.substring(1, inputValue.length);
}
return inputValue;
}
其他回答
我不得不承认,核实电话号码是一项艰巨的任务。 至于这个具体的问题,我会改变正则表达式从
/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)\d{4}$/
to
/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/
因为唯一多余的元素是最后的破折号/空格。
如果你想找10个数字,那就忽略除数字-以外的所有数字
return value.match(/\d/g).length===10;
这个reg ex适用于国际电话号码和多种格式的移动手机号码。
下面是正则表达式: / ^ (+ {1} \ d {2,3} \ s ? [(] {1} \ d {1,3} [)] {1} \ s ? \ d + | + \ d {1} {2,3} \ s \ d + | \ d +) {1} [\ s | - ? \ d + ([\ s | - ? \ d +){1,2} /美元
下面是JavaScript函数
function isValidPhone(phoneNumber) {
var found = phoneNumber.search(/^(\+{1}\d{2,3}\s?[(]{1}\d{1,3}[)]{1}\s?\d+|\+\d{2,3}\s{1}\d+|\d+){1}[\s|-]?\d+([\s|-]?\d+){1,2}$/);
if(found > -1) {
return true;
}
else {
return false;
}
}
这将验证以下格式:
+44 07988-825 465(用任意连字符组合代替空格,但+44后面必须只有空格)
+44(0) 7988-825 465(用连字符的任何组合代替空格,但连字符不能直接存在于(0)之前或之后,(0)之前或之后的空格不需要存在)
123 456-789 0123(以任何连字符组合代替空格)
123-123 123(以任何连字符组合代替空格)
123 123456(空格可以用连字符代替)
1234567890
所有格式都不能存在双空格或双连字符。
- ^ (() \ d {3} (?) (s - | \) ? d {3} (- | \ s) ?\ d{4} -美元
的吗?字符表示前一组匹配0次或1次。组(-|\s)将匹配-或|字符。
除了数字,什么都不用管。
以下是我使用的方法:
// USA phones should contain at least 10 digits. For Ukrainian phones it's OK to have only 9 digits, without leading 0 and country code prefix: [+380] 88 888-88-88.
String.prototype.isValidPhone = function(digitsCount) {
var str = this.trim().match(/\d/g);
return str && str.length >= (digitsCount || 10); // default is at least 10 digits
}