admin管理员组文章数量:1336631
I have a form in which people can enter file paths. I want to make sure that the paths they are entering point to pictures so here is what I thought would work.
function checkExt()
{
var extension= /* I know that the code to cut the extension off of the file
is working correctly so for now let's just go with it ok */
if(extension!="jpg" || "gif" || "bmp" || "png" || "whatever else")
alert("The file extension you have entered is not supported");
}
But this does not work. I have tracked it down to the if statement because if I select only 1 kind of file to check for, then it will work correctly. So my question to you is what the hell do I have to change to make this thing work correctly. I've been on this for about three hours now and it's driving me mad. Thanks for all of the help in advance.
I have a form in which people can enter file paths. I want to make sure that the paths they are entering point to pictures so here is what I thought would work.
function checkExt()
{
var extension= /* I know that the code to cut the extension off of the file
is working correctly so for now let's just go with it ok */
if(extension!="jpg" || "gif" || "bmp" || "png" || "whatever else")
alert("The file extension you have entered is not supported");
}
But this does not work. I have tracked it down to the if statement because if I select only 1 kind of file to check for, then it will work correctly. So my question to you is what the hell do I have to change to make this thing work correctly. I've been on this for about three hours now and it's driving me mad. Thanks for all of the help in advance.
Share Improve this question asked Aug 6, 2012 at 21:18 ChapmIndustriesChapmIndustries 1,5314 gold badges17 silver badges19 bronze badges 1- 1 You have to make the parison each time. – j08691 Commented Aug 6, 2012 at 21:20
6 Answers
Reset to default 6That's a syntax and a logic error. It should be:
if (extension != "jpg" &&
extension != "gif" &&
extension != "bmp" &&
extension != "png" &&
extension != "whatever else") {
// This will execute when the extension is NOT one of the expected
// extensions.
}
Furthermore, you could handle it a little more succinctly with a regular expression:
if (!/jpg|gif|bmp|png|whatever/.test(extension)) {
// This will execute when the extension is NOT one of the expected
// extensions.
}
Addendum:
The examples above execute the body of the if-statement when the value of extension
is not one of the supported values. If you wanted to execute the body of the if-statement when the value of extensions
is one of the supported values, you would change the logic from not equal/and to equal/or, like so:
if (extension == "jpg" ||
extension == "gif" ||
extension == "bmp" ||
extension == "png" ||
extension == "whatever else") {
// This will execute when the extension is one of the expected extensions.
}
And again, it'd be more concise using a regular expression:
// I just removed the leading ! from the test case.
if (/jpg|gif|bmp|png|whatever/.test(extension)) {
// This will execute when the extension is one of the expected extensions.
}
As an alternative to the verbosity, use a regex:
if (extension.search(/jpg|gif|bmp|png/) === -1) {
alert("The file extension you have entered is not supported.");
}
You should be using &&
instead of ||
and you must prefix the !=
operator with extension
on each condition, not just the first one:
if (extension != "jpg" &&
extension != "gif" &&
extension != "bmp" &&
extension != "png" &&
extension != "whatever else")
You need to include the parameters to pare in each clause.
if( extension != "jpg" ||
extension != "gif" ||
extension != "bmp" ||
extension != "png" ||
extension != "whatever else"){
//TODO: handle condition
}
As others have noted, this is a logical error in that if "jpg" is the extension the condition will still be hit because "jpg" != "gif". You might want to consider using &&
.
You have to pare the variable extension
for each tested extension. A pact way to do this would be:
var extension = whatever;
var supportedExtensions = ["jpg","gif","bmp","png",... whatever else];
if (supportedExtensions.indexOf(extension) < 0) alert("Unsupported extension");
The other answers have shown you how your javascript logic for multiple parisons is wrong. But, a much better way to do this is to use a javascript map like this:
function checkExt() {
var allowedExtensions = {
"jpg": true, "gif": true, "bmp": true, "png": true, "whatever else":true
};
var extension= /* I know that the code to cut the extension off of the file
is working correctly so for now let's just go with it ok */
if (allowedExtensions[extension] !== true) {
alert("The file extension you have entered is not supported");
}
}
The is a easier to maintain by just adding/removing items to/from the allowedExtensions
object.
本文标签: Javascript If statement used to check file extensions not workingStack Overflow
版权声明:本文标题:Javascript If statement used to check file extensions not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742408491a2469307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论