Alex Rivera | Logout

How can I check if my string contains a period in JavaScript?

Asked 2011-07-03T00:15:19.643
18

I want to be able to detect if a string has a . in it and return true/false based on that.

For example:

"myfile.doc" = TRUE

vs.

"mydirectory" = FALSE;
Edit
Report

1 Answer

0

Use indexOf. It returns an integer showing the position of a substring, or -1 if it isn't found.

For example:

var test="myfile.doc"
if (test.indexOf('.')) {alert("Period found!";}
else {alert("Period not found.  Sorry!";}
answered 2011-07-03T00:19:57.250

Your Answer