I have implemented a phone number validation function for a web app. However, when this information is displayed in a windows app using a specific function that populates data to controls, it only accepts phone numbers in the format of (123) 456-7890.
(123) 456-7890
The current function allows users to enter any 10-digit phone number format, such as 123-45-6789.
function validatePhone(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
if (fld.value == "") {
return false;
} else if (isNaN(parseInt(stripped))) {
return false;
} else if (!(stripped.length == 10)) {
return false;
}
return true;
}
I have researched online and found regular expressions for two formats (123) 456-7890 | 123-456-7890, such as (((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}. However, I need to restrict the validation to only one format with parentheses. Is there a way to modify this function to validate phone numbers in the exact format shown above? Thank you in advance.