TypeError: pattern.test is not a function

Encountered a similar issue.

Solution: use .match() instead of .test() method


Also, make sure to write /pattern/.test("string") as opposed to "string".test(/pattern/).


Your pattern holds a string, and not a regular expression.

Make it var pattern = /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/; (without quotes)


Your pattern must be RegEx literal (double quotes around that should not be there) like this

var pattern = /^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/;

Otherwise you need to use RegExp object, with proper escaping for \, like this

var pattern = new RegExp("^(()?\\d{3}())?(-|\\s)?\\d{3}(-|\\s)?\\d{4}$");