JS search and test error - "is not a function"

I’m trying to use a regular expression along with the javascript search or test functions to test a file extension. The method, however, returns a “is not a function” for either the search or test functions (javascript, not servoy).

Here is the code:

// Message just to check script
var staffphotoupload = 'Choose';

application.setStatusText(staffphotoupload);

var staffphotoupload = plugins.file.showFileOpenDialog( '1','PATH REMOVED');

// Message just to check returned file path
application.setStatusText(staffphotoupload);

// checks for valid file extension
var staffphotoisimage = ""
var image_regExp = /\.(gif|jpg|png)/;
staffphotoisimage = staffphotoupload.test(image_regExp)


if (staffphotoupload && staffphotoisimage) //sets the field if valid
{
staffphotouploadfile = plugins.file.readFile(staffphotoupload)
fs_staffphoto = staffphotouploadfile
}

It works perfectly without the staffphotoisimage test - except of course that then a user can upload unwanted file types.

Any idea why its spitting this error out at me? I’ve used other javascript / regExp combos using replace and match successfully, but the search and test don’t seem to work correctly.

Thanks for any info or insight…its Friday, I’m tired of looking at it :slight_smile:

Nolan M
FSCI

the showFileOpenDialog returns a JSFile object and not a String. The error you get is because a JSFile doesn’t have a “search” function (by the way: I don’t know the “test” function). Change your line to

staffphotoisimage = staffphotoupload.getName().search(image_regExp)

and it should work.

Yes…that works. I’d used the result of the dialog in a status message, and it showed the path - so wrongly assumed that’s what was returned.

So with it being a JSfile object returned, is there a better way to reference and save the binary part than what I did?

staffphotouploadfile = plugins.file.readFile(staffphotoupload) 

Lastly, the test function in javascript just tests for a RegExp match - if found, returns true, if not, false, versus the search which returns the index of where the match was found in the string.

Nolan M.
FSCI