Regex validation code for excel file upload jQuery

前端 未结 2 1130
旧巷少年郎
旧巷少年郎 2021-01-27 06:29

how to validate excel files

My JQuery

 jQuery(\"#excel\").validate({
      expression: \"if (VAL.match(/^([a-z]\\w*)\\.(xls[mx]?)$/) &am         


        
相关标签:
2条回答
  • 2021-01-27 07:08

    You need to escape the dots.

    ^([a-z]\w*)(\.xlsx|\.xlsm|\.xls)$
    

    OR

    ^([a-z]\w*)\.(xls[mx]?)$
    

    [mx]? matches an optional m or x

    Use this regex if you want to allow any characters inbetween ^([a-z].*)\.(xls[mx]?)$

    0 讨论(0)
  • 2021-01-27 07:21

    First of all, you should change an order in expression, since you don’t want to execute expression on invalid VAL:

    VAL && VAL.match(/^([a-z]\w*)(.xlsx|.xlsm|.xls)
    

    Secondary, any \w symbol is perfectly valid as starting symbol for filename, as well, as a dot and space (and, possibly, some other symbols.) Dots in regexps should be escaped. And, a last but not least, you could want to compact the xls*s:

    /^([\w\s.]*)\.xls[xm]?$/
    
    0 讨论(0)
提交回复
热议问题