I am fairly new to coding, especially Javascript, and am having difficulty getting my form to recognize phone numbers using parentheses. I have been able to make a code for accepting dashes between numbers to work, but the variations I have attempted with parentheses have all come back as invalid. Below is my functioning code with dashes. If possible, I was hoping to get some advice for how to adjust this code to incorporate parentheses. I am attempting to make the form function so that it can accept the following variations: xxx-xxx-xxxx , (xxx)xxxxxxx , and (xxx)xxx-xxxx.
function validateTenDigitsAndDashes (str)
{
//alert("in validateTenDigitsAndDashes: str, str.length = '" + str + "', " + str.length) ;
var retVal = 12 == str.length ;
for (var i = 0 ; retVal && (i < str.length) ; i++) {
//alert(" in loop: i, str.charAt(i) = " + i + ", " + str.charAt(i)) ;
if (3 == i || 7 == i) retVal = '-' == str.charAt(i)
else retVal = isDigit(str.charAt(i)) ;
}
return retVal ;
}