Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  32] [ 4]  / answers: 1 / hits: 32949  / 10 Years ago, sun, june 1, 2014, 12:00:00

Just trying to use javascript's regex capabilities with the .test() function.



  var nameRegex = '/^[a-zA-Z0-9_]{6,20}$/';

if(nameRegex.test($('#username').val())) {
...
}


The error is on this line if(nameRegex.test($('#username').val())) {



Debugger breaks there and says Uncaught TypeError: undefined is not a function. It seems like .test() is not defined? Shouldn't it be?


More From » jquery

 Answers
15

As it currently stands, nameRegex isn't a regex but a string and String doesn't have test functon which is why you are getting that error.



Remove the quotes around your regex. That is the literal form of regex.



var nameRegex = /^[a-zA-Z0-9_]{6,20}$/; //remove the quotes

[#70775] Thursday, May 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;