0

I did a script that check the image width and remove the image if the width is lesser than certain amount.

The image field is required and after I removed it, it still passed the file validation.

sample: http://jsfiddle.net/AUQYv/3/

<form action="">
    <input type="file" id="file" required />
    <input type="submit" value="Submit">
</form>

Javascript

var _URL = window.URL || window.webkitURL;

$("#file").change(function (e) {
    var file, img;


    if ((file = this.files[0])) {
        img = new Image();
        img.onload = function () {
            if(this.width<490||this.height<175){
            $("#file").val("").change();
            }
        };
        img.onerror = function () {
            alert("not a valid file: " + file.type);
        };
        img.src = _URL.createObjectURL(file);


    }

});

2 Answers 2

1

Your conditional statement is setting file = this.files[0]. Change to ==

Edit: Since both commenters pointed out that I was wrong, replace $("#file").val("").change():

with

$("#file").replaceWith($("#file").clone());
Sign up to request clarification or add additional context in comments.

6 Comments

This is not meant as a comparison, but as a value assignment. If this.files[0] is undefined, then the whole if block does not get executed – if it is not, then file has a value that can be worked with within.
Sorry, this is not the answer I want. The code is working, just the validation part after we removed the image. I set the image field is required and after I removed the image, it still passed the file validation.
I edited my answer with a different solution, since I was mistaken about your conditional statement.
Thanks. This answer worked but only for the first time. The 2nd time you upload the same image, it passed the validation jsfiddle.net/AUQYv/6
Well shoot, the change() method isn't being re-initialized when you enter one the first time, so the change() code isn't being called the second time.
|
0

Try with this.

<form action="" id="form" method="post">
    <input type="file" id="file" required accept='image/png, image/gif, image/jpeg, image/jpg'/>
    //your button submit
    <a href="#" id="submitForm">Submit</a>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
//When click the button
$('#submitForm').click(function(){
  var validation = true; //Var to validation true
  if($('#file').val()){ //There's image?
      if($('#file').width()<490 || $('#file').height()<175){
          validation = false;
      }
  }else{
     validation = false;
  }
  //Is true?
  if(validation){
    $('#form').submit();
  }else{
    alert("select a image");
  }
});
</script>

3 Comments

Sorry, this is not the answer I want. I set the image field is required and after I removed the image, it still passed the file validation.
sorry, but I don't understand you, can you be most specific with your question please?
The image is a required field. When you click submit without uploading an image, you can't proceed as it will be asking for an image. But after you uploaded an image that width lesser than 400px, the file is removed but you can proceed without uploading an image.

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.