0

Hello guys i am using a function to detect the image extension.

<body>
<input id="imgExtension" type="text" />
<input id="myFile" type="file" />
<script>
function checkExt(x){
var y = x.split('.').pop();
if (y != "jpg" && y != "png") {
    return false;
}

else {
    // i want to return *y*[image extension] from here
    return true;
}
}

$(function(){
$("#myFile").change(function(){
var x = $(this).val;
    if(checkExt(x)){
       // i want to put [image extension] here in #imgExtension
       $('#imgExtension').val();
    }

    else  {
       alert("Error, invalid image extension");
    }
}    
}
</script>
</body>

Recently, I was just checking the image extension and returning true or false but i want to get the extension as well because I need to parse it to my PHP script.

2
  • you could just return an object : return {valid: true, extension : y} Commented May 4, 2015 at 9:00
  • 1
    var x = $(this).val; should be var x = $(this).val(); Commented May 4, 2015 at 9:01

4 Answers 4

5

Instead of return true or false you can return just the extension or null if there is no extension at all.

In other hand you can create one js object that contains the two values, but i would go for the first.

Sign up to request clarification or add additional context in comments.

3 Comments

as u said return null/extension. Can i put it as returning false/extension ??
why do you want to return 2 completely different types? that is not a good practice, you can easily check if a value is null so it as better effect than just return true/false. If you want to have a well structured code returning just the extension/null is a better approach, but you can always return a object
how to work with the returned obeject, how to use it in if,else conditions ?
2

The easiest way to return more than one value is to return an object.

return { value: true, extension: '.jpg' };

You can then access the value returned as a normal object.

3 Comments

i don't think this is what he needs - he wants to return true so his check is correct
and how wud i verify then ? if(checkExt(x).value == false){ return false; } else{ $('#imgExtension').val(checkExt(x).extension); }
You could do so. A more common way to do it would be to save the response to a variable. var extCheck = checkExt(x); and then access it with if (extCheck.value) to see if it was true and extCheck.extension to access the extension.
0

You cannot return two things (unless you use an array). I suggest that you return either the extension or null. Because of something called falsfy in JavaScript, calling if(checkExt(x)) will evalulate to false for null and true for jpg or png.

Comments

0

You dont need to return 2 values. checkExt works as it should if you just return the value, or nothing at all. The cleanest way to do things like this is with a ternary statement (google it, easy to learn, will save hours of your life)

function checkExt(x){
  var y = x.split('.').pop();
  return y != "jpg" && y != "png" ? false : y;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.