Phone number validation

By the way, if you’re just wanting to validate the form without wanting to learn much about the details of how it’s done, there is really good jQuery [form validation plugin from "]form validation](http://docs.jquery.com/Plugins/Validation) that makes it a lot easier.

These examples help to demonstrate some of what it an do.
http://jquery.bassistance.de/validate/demo/milk/
http://jquery.bassistance.de/validate/demo/marketo/

Do you mean it like this?

function ValidatePhone(Phone)
{
    var isValid = true;
    if ((Phone.value == null)||(Phone.value == "" || isValid == false)) //if match failed
    {
        pValue = Phone.value;
        alert("Please Enter your Phone Number." + pValue);
        Phone.focus();
            return false;
    }


    if (checkRequirements(Phone.value) == false)
    {
        alert("Please Enter a Valid Phone Number.");
        Phone.value = "";
        Phone.focus();
            return false;
    }
    else    
        alert("YAY!");
    return true;
 }    

function ValidateForm()
{
    var isValid = ValidatePhone(document.getElementById("Text1"));     
    if (isValid == false)
    {
        return false;
    }
    isValid = ValidatePhone(document.getElementById("Text2"));
    if (isValid == false)
    {
        return false;
    }    
    isValid = ValidatePhone(document.getElementById("Text3));
    if (isValid == false)
    {
        return false;
    }    
    
} 

I can’t use jQuery. No, I just need Javascript.

Phone.value will give you the value in the form field that someone typed in.
Phone.id will give you the identifier associated with the field.

For example


<label>Business Phone: <input name="BusinessPhone" value=""></label>

Phone.name would be BusinessPhone, and Phone.value would be empty.
Once someone types something into the field and submits, then Phone.value would contain what they typed in.

Yes I understand this part. But are you asking me to put it as this?

function ValidatePhone(Phone)
{
    var isValid = true;
    if ((Phone.value == null)||(Phone.value == "" || isValid == false)) //if match failed
    {
        pValue1 = Text1.value;
        pValue2 = Text2.value;
        pValue3 = Text3.value;
        alert("Please Enter your Phone Number." + pValue1 + pValue2 + pValue3);
        Phone.focus();
            return false;
    }


    if (checkRequirements(Phone.value) == false)
    {
        alert("Please Enter a Valid Phone Number." + pValue);
        Phone.value = "";
        Phone.focus();
            return false;
    }
    else    
        alert("YAY!");
    return true;
 }

No, I think that you had it better with the previous code. There is absolutely no need to use of text1/text2/text3 inside the ValidatePhone function itself.

Okay, how do I change the codes in #42?

How do you change them? Please expand on what them to do.

What do you mean? I just need more advice on how to show which textboxes have not been filled in using alert message. Secondly, it will show a single “YAY” when all three are filled in correctly.

It sounds like you need to refactor things then, by moving the alerts out of the ValidatePhone fnction and into the ValidateForm function. Then you can use the information on what passed and what didn’t, and add the failed information on to a string, which you would then show at the end of validating all three.

Okay, can you guide me again?

My pumpkin has arrived to take me to beddy-bye land, so it won’t be me I’m afraid.

Good luck with it though, you’re making good progress.

Okay, please help me when you’re up and going.

I can’t promise anything as most of tomorrow I’ll be unconnected, out in the big blue room where there is the daystar.

I’ll go through how you would do this, by comparing how the existing code works, as against how you want the code to work.

Currently, the ValidatePhone function shows an alert, and return either true or false.
Currently, the ValidateForm function returns as soon as the ValidatePhone function returns false.

Instead of doing that, you want the ValidatePhone messages to be all added together, and shown at the end as one combined alert.

So, here’s how you will want to change the functions.

You need ValidatePhone to do this instead:

[list][]not do any alerts at all, as that job will be handled higher up
[
]when validation fails, return the error message
[*]when validation succeeds, return just an empty string[/list]

You also need the ValidateForm function to do this:

[list][]to string together the returned strings from each of the ValidatePhone functions
[
]if the added strings are an empty string, then validation is good
[*]otherwise, you know that the an error has occurred, and you can alert the error[/list]

Those are the refactoring changes that you need to make to your functions. The above two lists can be used as a checklist, as you work towards achieving that design goal.