简体   繁体   English

如何使用javascript验证电话号码?

[英]How do I validate a phone number with javascript?

Would someone a little smarter than myself be able to help me with this function? 有人比我自己聪明一点能帮助我这个功能吗? Its purpose is to validate a text input in a form, a phone number field that will only accept 0-9, dash and dot. 其目的是验证表单中的文本输入,电话号码字段只接受0-9,短划线和点。 The HTML calls the function fine. HTML调用函数很好。

function validateFeedback() {
    var phone = document.getElementById("phone");
    var validNumber = "0123456789.-";
    for (i = 0; i < phone.length; i++); {
        if (validNumber.indexOf(phone.charAt(i)) == -1); {
            alert("You have entered an invalid phone number");
            return false;
        }
    }
    return true;
}

Thanks so much for any help. 非常感谢您的帮助。

Regular expressions should help ;) 正则表达式应该有帮助;)

I'm sorry I haven't tried to run this code, but it should be OK. 对不起,我没有尝试过运行此代码,但应该没问题。

function validateFeedback(){
    var phone = document.getElementById("phone");
    var RE = /^[\d\.\-]+$/;
    if(!RE.test(phone.value))
    {
        alert("You have entered an invalid phone number");
        return false;
    }
    return true;
}

try like this: 试试这样:

function validateFeedback()
    {
    var phone = document.getElementById("phone");
    var validNumber = "0123456789.-";


    for(i = 0; i < phone.length; i++) {
            if(validNumber.indexOf(phone.charAt(i)) == -1) {
                    alert("You have entered an invalid phone number");
                    return false;
                }
            }

        return true;
    }

there are ; ; out of place ... 空间不足 ...

Try this one 试试这个吧

function validateFeedback(value) {
    var length = value.length;
    chk1="1234567890()-+ ";
    for(i=0;i<length;i++) {
        ch1=value.charAt(i);
        rtn1=chk1.indexOf(ch1);
        if(rtn1==-1)
            return false;
        }
        return true;
    }

I think you should use a regex to do this. 我认为你应该使用正则表达式来做到这一点。 Something link this: 东西链接这个:

function validateFeedback() {
  var phone = document.getElementById("phone").value;
  var reg = new RegExp("[0-9 .-]*"); 

  return reg.test(phone);
}

If the text input is in a form, you can reference it more directly using the form id and the element id: 如果文本输入在表单中,您可以使用表单id和元素id更直接地引用它:

var phone = document.<formId>.phone;

What you want to test is the value of the element, so you need: 您要测试的是元素的值,因此您需要:

var phone = document.<formName>.phone.value;

Since the function is probably called from a submit listener on the form, you can make things more efficient using: 由于该函数可能是从表单上的提交侦听器调用的,因此您可以使用以下方法提高效率:

<form onsubmit="return validateFeedback(this);" ...>

It also seems to me that a phone number has only digits, not "-" or "." 在我看来,电话号码只有数字,而不是“ - ”或“。”。 characters, so you should only test for digits 0-9. 字符,所以你应该只测试数字0-9。

So the function can be like: 所以功能可以是:

function validateFeedback(form) {
  var phoneValue = form.phone.value;

  // Use a regular expression to validate the value
  // is only digits
  if (/\D/.test(phoneValue) {
    // value contains non-digit characters
    // advise user of error then
    return false;
  }
}

you may want to test that the length is reasonable too, but note that phone numbers in different places are different lengths, depending on the location and use of area or country codes, and whether the number is for a mobile, landline or other. 您可能希望测试长度是否合理,但请注意,不同地方的电话号码长度不同,具体取决于地区或国家/地区代码的位置和使用情况,以及号码是否适用于移动电话,固定电话或其他。

I would prefer to use regular expressions for something like this. 我更喜欢使用正则表达式来做这样的事情。 Please look at my modified version of your function which should work in all major browsers without any framework. 请查看我的功能修改版本,该版本应该适用于所有主流浏览器而无需任何框架。

function validateFeedback() {
    // Get input
    var phone = document.getElementById("phone"),
        // Remove whitespaces from input start and end
        phone = (phone || '').replace(/^\s+|\s+$/g, ''),
        // Defined valid charset as regular expression
        validNumber = "/^[0123456789.-]+$/";

    // Just in case the input was empty
    if (phone.length == 0) {
        // This depends on your application - is an empty number also correct?
        // If not, just change this to "return false;"
        return true;
    }

    // Test phone string against the regular expression
    if (phone.match(validNumber)) {
        return true;
    }

    // Some invalid symbols are used
    return false;
}
function phonenumber(inputtxt)
{
  var phoneno = /^\d{10}$/;
  if((inputtxt.value.match(phoneno))
        {
      return true;
        }
      else
        {
        alert("message");
        return false;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM