Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript error: "val.match is not a function"

I’m using the match function with a Regular Expression.

The code I’m using is:

if(val.match(/^s+$/) || val == "" ) 

However, it produces the following error:

"val.match is not function" 

What is the problem?

like image 205
zahir Avatar asked Feb 03 '11 05:02

zahir


1 Answers

I would say that val is not a string.

I get the

val.match is not function

error for the following

var val=12;  if(val.match(/^s+$/) || val == ""){    document.write("success: " + val); } 

The error goes away if you explicitly convert to a string String(val)

var val=12;  if(String(val).match(/^s+$/) || val == ""){    document.write("success: " + val); } 

And if you do use a string you don't need to do the conversion

var val="sss";  if(val.match(/^s+$/) || val == ""){    document.write("success: " + val); } 
like image 89
chrisp7575 Avatar answered Sep 20 '22 08:09

chrisp7575