Magento file validation - prototype

Add file extension validation in magento form by using below code.
Validation.add('validate-jpg-png','Please upload only jpg/png file format!',function(the_field_value){
            //console.log(the_field_value);
            if(the_field_value == '') return true;
            var extension = the_field_value.replace(/^.*\./, '');
 
            if (extension == the_field_value) {
                extension = '';
            } else {
                extension = extension.toLowerCase();
            }
            switch (extension) {
                case 'jpg':
                    return true;
                case 'png':
                    return true;
                //you can add more case for valid extension.
                default:
                    return false;
            }
});
To make it work add validate-jpg-png class to input file type.

Comments

Popular posts from this blog

Send email from xampp localhost

Modify item price in cart after placing order using sales_quote_add_item

Convert long url to short url using tinyurl api