locked
Check file upload extension and file name format RRS feed

  • Question

  • User-73514677 posted

    Hi.

    How to check only zip files are uploaded in the file upload control and also check for a particular file name format .

    example: ABC120190117

    First 3 letters being alphabets,

    characters 5 to 8 being "YYYY" format ,

    characters 9,10 being "MM" format ,

    characters 11,12 being "DD" format

    Thanks

    Thursday, January 17, 2019 11:54 AM

All replies

  • User503812343 posted

    you can add jQuery validation method to validate file name format

    jQuery.validator.addMethod("uploadFile", function (val, element) {
    
             //validate file name using val variable
    
          }, "File type error");

    and on form submit you can do like this

     $(document).ready(function(){
                  $('#quote_form').validate({
          rules: {
               image: {
                  required: true,
                  extension:'zip',
                  uploadFile:true,
                  }
                 }
              });
           });

    Thursday, January 17, 2019 7:07 PM
  • User839733648 posted

    Hi venkatzeus,

    According to your description, I suggest that you could use Regular Expressions and .test() function to check the filename.

    I've made a sample on my side, and maybe you could refer to.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="text/javascript">
            function Checkfiles() {
                var fup = document.getElementById('filename');
                var fileName = fup.value;
                console.log(fileName);
                var ext = fileName.substring(fileName.lastIndexOf('\\') + 1);
                console.log(ext);
                var reg = /^[a-zA-Z]{3}(.)\1{3}(?!\1)(.)\2((?!\1|\2).)\3\.(zip)$/;
                reg.test(ext);
                if (reg.test(ext)) {
                    console.log("Format Right");
                   return true;
                } else {
                    fup.focus();
                    console.log("Format Wrong");
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <input type="file" id="filename" />
        <input type="button" onclick="Checkfiles()" value="CheckFilename"/>
    </body>
    </html>

    result:

    Besides, I suggest that you could refer to below link to check your regular expressions.

    https://regex101.com/

    Best Regards,

    Jenifer
     

    Friday, January 18, 2019 9:24 AM