0

I have written the following function in javascript to validate a string that I will use as a file name. I would like to check for all the characters that are restricted by Windows OS as invalid while creating files. I checked the regular expression at RegExr, it seems to be working as expected but it doesn't work when called from an Angular controller and it only matches the first character in the parameter. I'm adding the file extension later on so that isn't a problem.

Can anybody help with it? I'm relatively new to regular expressions and would appreciate any help or pointers to useful resources.

    function validateInput(value) {
        if (!AngularUtils.isUndefinedOrNull(value)) {
            var regex = new RegExp("[^<>/\\\\=|:*?\"]+$");
            var regexOutput = regex.test(value);
            if (!regex.test(value))
                return true;
        }
        return false;
    }

Edit: Even after changing the regex to handle javascript constructors, I'm still getting valid matches for the following input: "sample_css", "sample=css","=sample"

Only the first string should be valid. jsfiddle here.

Ritik Kumar
  • 61
  • 1
  • 9

0 Answers0