Validate max file upload size with parsley.js

前端 未结 3 1771
一个人的身影
一个人的身影 2021-01-25 19:10

I\'m using parsley.js in a USP Pro form in Wordpress. The validation works, in general, but now I\'m trying to validate the max file size and I don\'t really understand how to d

3条回答
  •  我在风中等你
    2021-01-25 19:38

    I was now struggeling about the same Problem for days and I now have found one possible workaround. The problem is (as mentioned above) that you dont get the parsleyField anymore but have only the requirement and value field. My idea is to use the requirement field and put there the name or ID of the input field together with the required filesize.

    This could look like:

    
    

    Then you should be able to write your own validator like so:

    window.ParsleyValidator
      .addValidator('filemaxsize', function (val, req) {
          var reqs = req.split('|');
          var input = $('input[type="file"][name="'+reqs[0]+'"]');
          var maxsize = reqs[1];
          var max_bytes = maxsize * 1000000, file = input.files[0];
    
          return file.size <= max_bytes;
    
      }, 32)
      .addMessage('en', 'filemaxsize', 'The File size is too big.')
      .addMessage('de', 'filemaxsize', 'Die Datei ist zu groß.');
    

    I didn't find a solution to use the placeholder %s in the messages yet since it now contains unclean data....

提交回复
热议问题