user1395909
user1395909

Reputation: 167

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. The HTML calls the function fine.

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.

Upvotes: 6

Views: 37705

Answers (7)

yanddam
yanddam

Reputation: 397

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

Upvotes: 1

Sunil Dodiya
Sunil Dodiya

Reputation: 2615

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;
    }

Upvotes: 1

Aleksandrenko
Aleksandrenko

Reputation: 3065

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 ...

Upvotes: 2

GodLesZ
GodLesZ

Reputation: 919

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;
}

Upvotes: 1

Jiř&#237; Hern&#237;k
Jiř&#237; Hern&#237;k

Reputation: 2477

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;
}

Upvotes: 9

RobG
RobG

Reputation: 147353

If the text input is in a form, you can reference it more directly using the form id and the element 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.

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.

Upvotes: 1

Nicolas BADIA
Nicolas BADIA

Reputation: 5852

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);
}

Upvotes: 1

Related Questions