I create a form that contains a button to add a group of input fields dynamically one of them is an input file "I want to validate its size before uploading"
jQuery:
$('.formLayout .moreSubjects').click(function(){
var box_html = $('<div id="more-subjects"><br><label>Subject</label><input type="text" name="subject['+numberIncrC+']" dir="rtl" class="subject_list"><br><label>Attachment</label><input type="file" name="attachment['+numberIncrC+']" id="subjectToUpload'+numberIncrC+'" class="att_list"><label id="lbSize'+numberIncrC+'"></label><br><label>Status</label><select name="status['+numberIncrC+']" class="status_list"><option value="none"> </option><option value="approved">Approved</option><option value="rejected">Rejected</option><option value="underStudy">Under study</option></select><br><label>Date</label><input type="date" name="deadline['+numberIncrC+']"><br><label>Comments</label><textarea rows="4" cols="50" name="comments['+numberIncrC+']" dir="rtl"></textarea><button class="remove-box">Delete</button></div>');
box_html.hide();
$('.more-subjects').append(box_html);
box_html.fadeIn('slow');
$('.subject_list').each(function () {
$(this).rules("add", {
required: true,
messages: {
required: ".."
}
});
});
$('.att_list').each(function () {
$(this).rules("add", {
required: true,
accept: "doc|docx|pdf",
messages: {
required: "..",
accept: "..",
}});
$('select.status_list').each(function () {
$(this).rules('add', {
checkStatus: true
});
jQuery.validator.addMethod('checkStatus', function (value) {
return (value != 'none');
}, ".."); });
});
numberIncrC++;
return false;
});
File size method:
jQuery.validator.addMethod('checkSize', function ()
{
if(!$("#subjectToUpload"+numberIncrC).val() == '')
{
var iSize = ($("#subjectToUpload"+numberIncrC)[0].files[0].size / 1024);
if (iSize / 1024 > 1)
{
if (((iSize / 1024) / 1024) > 1)
{
iSize = (Math.round(((iSize / 1024) / 1024) * 100) / 100);
$("#lbSize"+numberIncrC).html( iSize + " GB");
}
else
{
iSize = (Math.round((iSize / 1024) * 100) / 100)
$("#lbSize"+numberIncrC).html( iSize + " MB");
}
}
else
{
iSize = (Math.round(iSize * 100) / 100)
$("#lbSize"+numberIncrC).html( iSize + " KB");
}
return (iSize<4294967295 ); // 4GB is 4,294,967,295 bytes
}
return true;
}, "..");
Now where should I call this method to check the size of each file ?
Aucun commentaire:
Enregistrer un commentaire