UnKnown UnKnown - 1 month ago 9
jQuery Question

jQuery validation not working after uploaded image is removed

In the below fiddle

http://jsfiddle.net/Luf0ks9b/22/

Step 1. Select any Image and Upload it

Step 2. Click On the Remove Button

and now click on Submit button , Validation for input type file gets failed ??

Its not asking for input file required

This is my code

jQuery(document).ready(function()
{
validatePacInsertForm();
});

$("#previewpic").change(function(){

$("#previewpic").blur().focus();
});

function validatePacInsertForm()
{
$('#pacinsertform').validate(
{
rules:
{
previewpic:
{
required: true
}
},
messages:
{
previewpic:
{
required: "Upload Image required",
}
},
highlight: function(element)
{
$(element).parent().addClass('error')
},
unhighlight: function(element)
{
$(element).parent().removeClass('error')
},
submitHandler: function(event, validator)
{
if ($("#pacinsertform").valid())
{
ajaxInsertPac();
return false;
}
}
});
}

function ajaxInsertPac()
{
alert('ajax call heer');
return false;
}

Answer

I've updated your fiddle, it was removing name attribute of the input which was used for validation, I've just put it back when remove button is pressed so validation is now working as it should.

$(document).on("click", ".removepic", function(event) {

    $("#previewpic").attr('name', 'previewpic');

});
Comments