jquery - Javascript regex .test() "Uncaught TypeError: undefined is not a function" -


just trying use javascript's regex capabilities .test() function.

  var nameregex = '/^[a-za-z0-9_]{6,20}$/';    if(nameregex.test($('#username').val())) {       ...   } 

the error on line if(nameregex.test($('#username').val())) {

debugger breaks there , says "uncaught typeerror: undefined not function". seems .test() not defined? shouldn't be?

as stands, nameregex isn't regex string , string doesn't have test functon why getting error.

remove quotes around regex. literal form of regex.

var nameregex = /^[a-za-z0-9_]{6,20}$/; //remove quotes 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -