Validation 万无一失验证中的复杂自定义验证

Validation 万无一失验证中的复杂自定义验证,validation,foolproof-validation,Validation,Foolproof Validation,我从开始在我的应用程序中实现了复杂的自定义万无一失的验证,但遗憾的是它不起作用。我的要求很简单,我有一个输入文件用于上传图像,如果用户选择上传下面指定以外的文件,则应该有一个验证 ".jpg",".png",".gif",".jpeg" 代码为 [Required(ErrorMessage = "Please upload Photo", AllowEmptyStrings = false)] [IsValidPhoto(ErrorMessage="Pl

我从开始在我的应用程序中实现了复杂的自定义万无一失的验证,但遗憾的是它不起作用。我的要求很简单,我有一个
输入文件
用于上传图像,如果用户选择上传下面指定以外的文件,则应该有一个
验证

            ".jpg",".png",".gif",".jpeg"
代码为

    [Required(ErrorMessage = "Please upload Photo", AllowEmptyStrings = false)]
    [IsValidPhoto(ErrorMessage="Please select files of type .jpg,.png,.gif,.jpeg")]
    public HttpPostedFileBase PhotoUrl { get; set; }

public class IsValidPhotoAttribute : ModelAwareValidationAttribute
{
    //this is needed to register this attribute with foolproof's validator adapter
    static IsValidPhotoAttribute() { Register.Attribute(typeof(IsValidPhotoAttribute)); }

    public override bool IsValid(object value, object container)
    {
        if (value != null)
        {
            string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".jpeg" };
            var file = value as HttpPostedFileBase;


            if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
            {
                return false;
            }


        }
        return true;

    }
}
CSHTML是

 @Html.TextBoxFor(m => m.PhotoUrl, new { @class = "form-control imgUpload", 
 @placeholder = "Please upload Photo", @id = "txtPhoto", @type = "file" })  
 @Html.ValidationMessageFor(m => m.PhotoUrl)

您将无法获得客户端验证,除非您还创建了一个脚本来添加规则。无需使用万无一失的方法,以下方法和脚本将为您提供服务器端和客户端验证

公共类文件AttachmentAttribute:ValidationAttribute,IClientValidable { 私有列表_扩展{get;set;} private const string _DefaultErrorMessage=“仅允许具有以下扩展名的文件类型:{0}”; 公共文件AttachmentAttribute(字符串文件扩展名) { _Extensions=fileExtensions.Split(“|”).ToList(); ErrorMessage=\u DefaultErrorMessage; } 受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext) { HttpPostedFileBase file=值为HttpPostedFileBase; 如果(文件!=null) { var isValid=_Extensions.Any(e=>file.FileName.EndsWith(e)); 如果(!isValid) { 返回新的ValidationResult(string.Format(ErrorMessageString,string.Join(“,”,_扩展名)); } } 返回ValidationResult.Success; } 公共IEnumerable GetClientValidationRules(ModelMetadata元数据、ControllerContext上下文) { var规则=新ModelClientValidationRule { ValidationType=“文件附件”, ErrorMessage=string.Format(ErrorMessageString,string.Join(“,”,_扩展名)) }; rule.ValidationParameters.Add(“扩展名”,string.Join(“,”,_扩展名)); 收益率-收益率规则; } } 剧本

$.validator.unobtrusive.adapters.add('fileattachment',['extensions',函数(选项){
var params={fileattachment:options.params.extensions.split(',')};
options.rules['fileattachment']=params;
if(options.message){
options.messages['fileattachment']=options.message;
}
});
$.validator.addMethod(“文件附件”,函数(值、元素、参数){
var扩展=getExtension(值);
返回$.inArray(扩展名,参数文件扩展名)!=-1;
});
函数getExtension(文件名){
变量扩展名=(/[..]/.exec(文件名))?/[^.]+$/.exec(文件名):未定义;
if(扩展名!=未定义){
返回扩展[0];
}
返回扩展;
};
然后将其用作

[文件附件(“jpg | gif | png | jpeg”)]
公共HttpPostedFileBase照片URL{get;set;}

当您的问题是关于jQuery验证插件时,仅使用jQuery验证标签。编辑。