9 years ago
3
Topic

Hi
I created a form with seblod and I add a filed for upload Image,
this filed is required, but when the user upload the wrong format,
the form is saved and then he gets an error for wrong format of file,
how can I set ajax validation that user cannot save form until he upload the right format of file or images

Thanks

Get a VIP membership
9 years ago
1
Level 1

Hello raz,

for the time SEBLOD don't implement the Ajax Upload. It's something in developpment progress but not finished.


By cons, you can eventually add some jquery code to check the extension of the image, and for modern browser, yoo can also check the file size.

Best regards.

Lionel

raz
9 years ago
0
Level 2

1-I think it is a bug, because if there isn't any javascript validation or javascript is disabled with user, It mustn't save data,
but the form is saved and show thank you message.

2- however I used this code to show alert message to user,
but I don't know how to prevent form from submitting,

$('#button_save_close').click(function(e){
var file = $('input[type="file"]').val();
var exts = ['jpg','jpeg'];
// first check if file field has any value
if ( file ) {
// split file name at dot
var get_ext = file.split('.');
// reverse name to check extension
get_ext = get_ext.reverse();
// check file type is valid as given in 'exts' array
if ( $.inArray ( get_ext[0].toLowerCase(), exts ) > -1 ){
alert( 'Allowed extension!' );
} else {
alert( 'Invalid file!' );
}
}
});

please help me how to do this.

310 Posts
ricco
9 years ago
0
Level 1

Hi raz,

I have the same problem. Did you find a solution?

I've found a script to check the size but the form is still saved even if an image with more than the allowed size is selected, and then the title of the image is rendered in the Content view with CSS borders, as the image is missing.

function validateSize(fileInput)
{
var fileObj, size; if ( typeof ActiveXObject == "function" )
{fileObj = (new ActiveXObject("Scripting.FileSystemObject")).getFile(fileInput.value);}
else {fileObj = fileInput.files[0];}
size = fileObj.size;
if(size > 1 * 1024 * 1024)
{alert('Max File size - 1 MB and less than 1024 х 1024. Resize please image and try again');
fileInput.parentNode.innerHTML = fileInput.parentNode.innerHTML; }
}
jQuery('#FIELD_NAME').change(function () {validateSize(this);});

Regards,

ricco

Get a Book for SEBLOD