Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: pattern.test is not a function

I'm writing a register form which has a ton of javascript included. While writing the basic functions to check the format of certain strings, I came across this problem (I've tried to solve it by myself but no luck there). In this simplified version I have three functions: one to check the format of phone number, second to check the format of email and third to combine those two functions to make it more clear. Here's the code:

<html> <head>     <meta charset="utf-8">     <script>         function filterPhone(phone){             var pattern = "/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/";             return pattern.test(phone);         }          function filterEmail(email) {             var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;             return regex.test(email);         }          function checkForm(){             var puh = document.getElementById("puh").value;         var email = document.getElementById("email").value;             if(filterPhone(puh) && filterEmail(email)){                 alert("It works");                 return true;             } else {                 alert("Check the format!");                 return false;             }         }     </script> </head> <body> <form method="post" action="" name="regForm" onsubmit="return checkForm()">     <input type="text" name="puh" id="puh" placeholder="Phonenumber"><br>     <input type="text" name="email" id="email" placeholder="Email address"><br>     <input type="submit" value="Check!"> </form> </body> </html> 

If I only use the function filterEmail it works fine but when I use them both (or even only the filterPhone!) it gives me an error: "TypeError: pattern.test is not a function" ("pattern" referring to the first function's (filterPhone) variable name called pattern). I've tried using exec() instead of test() and changing the regex-pattern, neither have worked. This is rather weird since the syntax seems to be correct. Why could be the reason?

like image 827
elgis Avatar asked May 04 '14 13:05

elgis


People also ask

Is not a function in test?

Conclusion # The "test is not a function" error occurs when the test() method is called on a value that is not a regular expression, e.g. a string. To solve the error, make sure to only call the test method on regular expressions, e.g. /[a-b]/. test('abc') .

Is not a function error JS?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.


2 Answers

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

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

like image 24
Gaurang Tandon Avatar answered Sep 17 '22 12:09

Gaurang Tandon


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}$"); 
like image 139
thefourtheye Avatar answered Sep 18 '22 12:09

thefourtheye