开发者

JQuery validation working individually but not together

*EDIT**

i tried it with && instead of || and now it does not come back as false for any file. I do think it needs to be && though.

I have a custom validation function that I am using to check if the file is an excel file. When i test is the last 4 characters are .xls OR the last 5 characters are .xlsx it works but when i check for both it does not. Any idea why it will not let me do this?

$.validator.addMethod("xlsxOrxls", function(value, element) {
    var isValid = true;
    var xlsx = value.substr(value.length - 5);
    var xls = value.substr(value.length - 4);
    if (xls != '.xls' || xlsx != '.xlsx') 
        isValid = false;
    return isValid;
}, "<br/><label style='color:red'><b>Not a valid file format.</b></label>"
);

If I comment out the var xlsx line and take out the "|| xlsx != '.xlsx'" part of the if statement it works and vice versa, but if i leave them both in there it will not work. I have even tried making them 2 separate f开发者_如何转开发unctions but thats not working either.

Any idea why its not letting me do this?


Use regex instead!

$.validator.addMethod(
    "xlsxOrxls",
    function(value, element) {
        return value.match(/\.xlsx?$/);
    },
    "<br/><label style='color:red'><b>Not a valid file format.</b></label>"
);

Or, if you really want do do it your way:

$.validator.addMethod(
    "xlsxOrxls",
    function(value, element) {
        var xlsx = value.substr(value.length - 5);
        var xls = value.substr(value.length - 4);
        return xls == '.xls' || xlsx == '.xlsx';
    },
    "<br/><label style='color:red'><b>Not a valid file format.</b></label>"
);


I think you are looking for &&, not ||. if the extension is different from .xls and its also different from .xlsx, then isValid is false.


You've got mutually exclusive conditions, but you're trying to check both of them at once. If the last 5 characters is ".xlsx" then the last 4 will never be ".xls" and vice versa.

Check for one condition and if it doesn't pass, check the other.


I was facing the same issue for Validation on xls and xlsx type files. I tried this easiest & simplest way..

<input id="fileSelect" type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />

Valid Accept Types:

For CSV files (.csv), use:

<input type="file" accept=".csv" />

For Excel Files 2003-2007 (.xls), use:

<input type="file" accept="application/vnd.ms-excel" />

For Excel Files 2010 (.xlsx), use:

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />

For Text Files (.txt) use:

<input type="file" accept="text/plain" />

For Image Files (.png/.jpg/etc), use:

<input type="file" accept="image/*" />

For HTML Files (.htm,.html), use:

<input type="file" accept="text/html" />

For Video Files (.avi, .mpg, .mpeg, .mp4), use:

<input type="file" accept="video/*" />

For Audio Files (.mp3, .wav, etc), use:

<input type="file" accept="audio/*" />

For PDF Files, use:

<input type="file" accept=".pdf" /> 

reference from Dom's Answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜