Question T7943
Visible to All Users

File name regex validation

created 4 years ago (modified 4 years ago)

In my angular app, when user tries to upload file using file widget or in matrix widget, I want to enforce a validation on file name by applying my regex so that it should only allow files matching with the pattern, else it should throw an error message. I am facing challenge on how to do this check in angular app. I understood so far is that I need to write something like below in my survey.component.ts

Code
surveyModel.onUploadFiles.add((survey, options) => {       const question = survey.getQuestionByName(options.name);        options.files.forEach(file => {          // check my file name is against my regex and accept only if it matches, else throw error.        })     })

I am not sure how to write custom regex validator and do check here. Any help much appreciated! Thanks.

Answers approved by surveyjs Support

created 4 years ago

Hello,

You can check the file.name against your regexp and whether to upload a file or skip it.

Thanks, Serge
SurveyJS Team

    Show previous comments (4)

      Hello,

      Here is the working plunker - https://plnkr.co/edit/Y94EIGByf42VBayX

      It sets the errors property of a question:

      JavaScript
      survey .onUploadFiles .add(function (survey, options) { var formData = new FormData(); var errors = []; options .files .forEach(function (file) { formData.append(file.name, file); errors.push(new Survey.CustomError("Too biiiiiig file")); }); if(errors.length > 0) { options.question.errors = errors; options.callback("error", errors); } else { options.callback("success", options.files.map(function (file) { return { file: file, content: "https://surveyjs.io/Content/Images/examples/image-picker/lion.jpg" }; })); } });

      Thanks, Serge
      SurveyJS Team

        Awesome. This works like a Charm! One more question. is there a way that I can show error message using html format or we don't have provision to show messages in desired html formats like below?

        The file name is invalid. please use below characters only

        • Uppercase
        • Lowercase
        • Numbers

          Hello,

          I created a separate ticket on your behalf: T7967: Is there a way that I can show error message using html format?. We placed it in our processing queue and will process it shortly.

          Thanks, Serge
          SurveyJS Team