Javascript jQuery 使用 RegEx 验证电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5692232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery validate phone number with with RegEx
提问by Brooke.
I have a simple ajax form and I'm trying to validate that it
我有一个简单的 ajax 表单,我正在尝试验证它
- has a value
- that value is a 10 digit number
- 有一个价值
- 该值是一个 10 位数字
I'm trying to use RegEx to do so. Here is what I have so far.
我正在尝试使用 RegEx 来做到这一点。这是我到目前为止所拥有的。
var reg = new RegExp("/[0-9]{10}/");
$("#call_form").bind("submit", function() {
if ($("#call_number").val().length < 1 && reg.test($("#call_number").val())) {
$("#call_error").show();
return false;
}
});
I know the problem has to do witht he RegExp as if I remove this portion of the code it validates that the box has a value.
我知道问题与他的 RegExp 有关,好像我删除了这部分代码,它验证了该框是否具有值。
EDIT: Here is the final regex I'm using
编辑:这是我使用的最后一个正则表达式
var regEx = new RegExp("/[0-9]/");
$("#call_form").bind("submit", function() {
if ($("#call_number").val().length != 10 && !$("#call_number").val().match(regEx)) {
$("#call_error").show();
$.fancybox.resize();
return false;
}
});
EDIT 2Using the suggestions here is what i'm usign which allows spaces and dashes that then get stripped on check
编辑 2使用这里的建议是我使用的,它允许空格和破折号然后在检查时被剥离
$("#call_form").bind("submit", function() {
var Phone = $("#call_number").val().replace(/\D+/g,'');
if (Phone.length != 10) {
$("#call_error").show();
$.fancybox.resize();
return false;
}
});
回答by beatgammit
Your regex works fine for me... you could shorten it to just /[0-9]{10}/
.
您的正则表达式对我来说很好用……您可以将其缩短为/[0-9]{10}/
.
Your problem is here:
你的问题在这里:
$("#call_number").val().length < 1
. If the number is 10 characters long, it will never be less than 1, no?
$("#call_number").val().length < 1
. 如果数字是 10 个字符长,它永远不会小于 1,不是吗?
You probably meant something like this:
你的意思可能是这样的:
$("#call_number").val().length === 10
$("#call_number").val().length === 10
回答by RobG
No one has said what was wrong with your original effort - it's the slashes (/). When calling RegExpas a constructor, you don't need the slashes (which are a token to indicate a regular expression litteral), e.g.:
没有人说过你最初的努力有什么问题 - 这是斜线 (/)。当调用RegExp作为构造函数时,您不需要斜杠(这是表示正则表达式的标记),例如:
var re = /\w+/i;
is equivalent to:
相当于:
var re = new RegExp('\w+','i');
Note that you have to quote backslashes for special characters.
请注意,您必须为特殊字符引用反斜杠。
One last thing - allow spaces in the number. You might remove them before testing or storing though. Users find it much easier to read numbers in blocks of 3 or 4 digits, e.g.
最后一件事 - 在数字中允许空格。不过,您可以在测试或存储之前将其移除。用户发现读取 3 位或 4 位数字块中的数字要容易得多,例如
- 1234 871 098 is easier to read than 1234871098.
- 1234 871 098 比 1234871098 更容易阅读。
回答by foxybagga
Here is what I use - its simple, just posting if someone is searching for the same.
这是我使用的 - 它很简单,如果有人正在搜索相同的内容,只需发布即可。
var a = PHONE_FROM_FIELD;
var filter = /^[0-9-+]+$/;
if (filter.test(a)) {
return true;
}
else {
return false;
}
Cheers!
干杯!
回答by Naveed Ahmad
something like this:
像这样:
var regEx = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/;
$("#call_form").bind("submit", function() {
var val = $("#call_number").val();
if (!val.match(regEx)) {
$("#call_error").show();
return false;
}
});
回答by Anil Singhania
function validate_Phone_Number() {
var number = $('field_Id').val();
var filter = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;
if (filter.test(number)) {
return true;
}
else {
return false;
}
}
回答by Anil Singhania
// check phone number validation
function validatePhoneNumber(number)
{
count=number.length;
if(number[0]!=" " && number[0]!="-" && number[count-1]!=" " && number[count-1]!="-")
{
temp=number.replace(" ", "");
temp=temp.replace("-", "");
if($.isNumeric(temp))
{
if(temp.length>=7 && temp.length<=12)
{
flag=1;
for(i=1;i<count;i++)
{
if(number[i]=="-" || number[i]==" ")
{
if(number[i-1]=="-" || number[i-1]==" " || number[i+1]=="-" || number[i+1]==" ")
{
flag=0;
}
}
}
if(flag==1)
{
valid=1;
}
else
{
valid=0;
}
}
else
{
valid=0;
}
}
else
{
valid=0;
}
}
else
{
valid=0;
}
return valid;
}