Websnippetz

Get cool snippets on php, css, jQuery, Magento and wordpress

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 <code>validate-jpg-png</code> class to input file type.  <pre name="code"><br /></pre>

karthi s

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: