-2

Hello I am having trouble with my javascript validation. I am trying to validate a Phone number which I want it to display only numbers and single spaces.

My Javascript code:

<script type="text/javascript">
function validateForm()
{
var x=document.forms["checkout_details"]["phone"].value;

        if (!isNaN(phone).value)//if the entered phone number is not a number
        {
            alert("Please enter a valid phone number");
            ret = false;//return false, form is not submitted
        }
}
</script>

HTML/PHP Code:

echo '<form name="checkout_details" action="confirm.php" onsubmit="return validateForm()" method="post">';
echo '<font color="red">*</font>&nbsp;<b>Phone Number:</b> <input type="text" name="phone" id="phone"><br /><br />';
echo '<input type="submit" value="Purchase">';
</form>

Anyone help me out tell me what i'm doing wrong. Thanks.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
  • 2
    I want to purchase something, but none of my phone numbers can be written using only numbers and spaces. Well, I think I'll go purchase the product somewhere else. – Arseni Mourzenko May 28 '14 at 13:49
  • 2
    your function doesn't return anything – Alexandru Chichinete May 28 '14 at 13:49
  • What am i missing? How do i fix it? – user3674827 May 28 '14 at 13:51
  • First, you need to decide on what you believe are acceptable characters. Numbers only as in 1234567890? Are spaces allowed? Hyphens allowed? Then, you can use javascript regex to check for the proper format – The One and Only ChemistryBlob May 28 '14 at 13:52
  • @MainMa What format is your number in? NANP only requires numbers (doesn't even need spaces). – Alex W May 28 '14 at 14:28
  • @AlexW: what I meant is that *strictly* controlling valid phones or postal codes is usually a bad idea, because you'll always end up with an Italian customer who registered on the website when he lived in USA, then moved to India, and now is ordering a product to ship to his girlfriend who lives in Germany. It's also very frustrating to copy-paste a phone number using international format such as +33 (0)1 23 45 67 89 and to be forced to retype it manually because some stupid website doesn't recognize this as a valid French phone number (while it *is* perfectly valid). – Arseni Mourzenko May 28 '14 at 15:16

3 Answers3

0

This is incorrect:

if (!isNaN(phone).value)//if the entered phone number is not a number

That actually evaluates to if it IS a number (double negative makes a positive). Also, ret = false; should be return false;

To remove spaces from the input before checking if it is valid, see this answer.

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
0

Your function needs to return true or false. A false return will prevent your form from posting.

So change this:

ret = false;//return false, form is not submitted

to:

return false;//return false, form is not submitted

Additionally, you cannot rely on isNaN to validate this field. In addition to not taking into account any phone numbers with spaces or other characters, it will not work correctly in some cases, and in this case you are negating its return value (!isNaN(x)), which means it won't work at all unless you fix that. You are better off using a regular expression to validate this field.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
  • 1
    Additionally, I wouldn't validate phone numbers by checking if the entered value is not a number. This will invalidate any phone number entered with spaces, `(`, `)`, `+`, etc. – ElGavilan May 28 '14 at 13:54
  • Hello i tried this but it still doesn't work. Any suggestions? – user3674827 May 28 '14 at 14:18
0

Try this

function validateForm()
{
var x=document.forms["checkout_details"]["phone"].value;

        if (!isNaN(x))
        {
            alert("Please enter a valid phone number");
            return false;
        }
        else
           return true;
}
Sid M
  • 4,354
  • 4
  • 30
  • 50