Regex validation code for excel file upload jQuery

2020-05-07 06:26发布

how to validate excel files

My JQuery

 jQuery("#excel").validate({
      expression: "if (VAL.match(/^([a-z]\w*)\.(xls[mx]?)$/) && VAL) return true; else return false;",
      message: "Please upload valid excel file"
 });

2条回答
够拽才男人
2楼-- · 2020-05-07 07:06

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]?$/
查看更多
爷的心禁止访问
3楼-- · 2020-05-07 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]?)$

查看更多
登录 后发表回答