//Validate phone number for 10 digit US numbers.

//phoneField - The HTML input field containing the phone number to validate.

//format - Integer value that defines how to format the text field.

function validatePhone(phoneField, format) {

   var num = phoneField.value.replace(/[^\d]/g,'');

   if(num.length != 10) {

        //Alert the user that the phone number entered was invalid.

        alert('Please enter a valid phone number including area code Format (xxx)-xxx-xxxx');                   

   } else {

        //Email was valid.  If format type is set, format the Phone to the desired style.

      switch(format) {

            case '0': //Format (xxx)-xxx-xxxx
				
               phoneField.value = "(" + num.substring(0,3) + ")-" +

                                    num.substring(3, 6) + "-" + num.substring(6);

               break;

            case '1': //Format xxx-xxx-xxxx

               phoneField.value = num.substring(0,3) + "-" +

                                    num.substring(3, 6) + "-" + num.substring(6);

               break;

            default: //Format xxxxxxxxxx

               phoneField.value = num;

               break;

        }

   }

}

