Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Joel 23 posts 108 karma points
    Oct 04, 2017 @ 03:01
    Joel
    0

    Custom validation jpg only

    Can anyone shed some light on why the following regex fails when implemented? I am trying to limit the file upload field to only accept jpg/JPG. I'm choosing "...or enter custom validation" and entering:

    ([^\s]+(\.(?i)(jpg))$)
    

    Is there a particular format the expression needs to be entered in?

  • Martin Aguirre 42 posts 210 karma points
    Oct 04, 2017 @ 11:57
    Martin Aguirre
    0

    Hi Joel.

    Where do you want to add this validation? I make a custom validation control inherited from normal fileupload. The control can be used in a Form.

    enter image description here

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using Umbraco.Forms.Core;
    using System.Collections.Specialized;
    using System.Configuration;
    
    namespace YourNameSpace
    

    {

    public class CustomFileUpload : Umbraco.Forms.Core.Providers.FieldTypes.FileUpload
    {
        private int _maxFileSize = 5242880;
        private string _ContentType = "application/pdf";
    
        public CustomFileUpload()
        {
            Id = new Guid("d8d89001-57ce-4b52-9005-c0c84f6651cb");
            Name = "Custom file upload";
            Description = "Limita a subir solo PDF o JPG y de hasta 1 MB";
            Icon = "icon-cloud-upload ";
            DataType = FieldDataType.String;
            FieldTypeViewName = "FieldType.CustomFileUpload.cshtml";
        }
    
    
        NameValueCollection coleccion = (NameValueCollection)ConfigurationManager.GetSection("customfileUploadSettings/tipoCustomfileUploadSettings");
        public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context)
        {
            IList<HttpPostedFileBase> files = context.Request.Files.GetMultiple(field.Id.ToString());
            foreach (HttpPostedFileBase file in files)
            {
                int cantidad = 0;
                int.TryParse(coleccion.Get("MaxFileSize"), out cantidad);                
    
                if (file.ContentLength > cantidad)
                {
                    return new[]
                    {coleccion.Get("MaxFileSizeMsg")};
                }
                else
                {
                    if (file.ContentType != coleccion.Get("ContentTypePDF"))
                    {
                        if (file.ContentType == coleccion.Get("ContentTypeIMG"))
                        {
                            string ext = Path.GetExtension(file.FileName).ToUpper();
                            if (ext != coleccion.Get("AllowExtension"))
                            {
                                return new[]
                                        {  coleccion.Get("ContentTypeErroMsg")};
                            }
                        }
                        else
                        {
                            return new[]
                            { coleccion.Get("ContentTypeErroMsg")};
                        }
                    }
                }
    
                if (file.ContentLength == 0)
                { return Enumerable.Empty<string>(); }
    
            }
            return Enumerable.Empty<string>();
        }
    }
    

    }

  • Joel 23 posts 108 karma points
    Oct 04, 2017 @ 21:55
    Joel
    0

    Gracias Martin.

    I wanted to add the simple validation as shown below.

    Whilst your method looks good, I need it to work with the Image Cropper datatype. Sorry, I should have been more specific, I didn't mean the file upload datatype.

    Custom validation Media Cropper

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies