If the text input is on the form, you can refer to it more directly using the form identifier and element identifier:
var phone = document.<formId>.phone;
What you want to check is the value of the element, so you need to:
var phone = document.<formName>.phone.value;
Since the function is probably called from the submit listener on the form, you can make things more efficient by using:
<form onsubmit="return validateFeedback(this);" ...>
It also seems to me that the phone number has only numbers, not a "-" or ".". characters, so you should only check the numbers 0-9.
So the function could be like this:
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 can check that the length is also reasonable, but note that phone numbers in different places of different lengths, depending on the location and use of the area code or country, and whether the number is for mobile, landline or others.
source share