Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVC自定义属性javascript部件不工作_Javascript_C#_Unobtrusive Validation - Fatal编程技术网

MVC自定义属性javascript部件不工作

MVC自定义属性javascript部件不工作,javascript,c#,unobtrusive-validation,Javascript,C#,Unobtrusive Validation,我创建了一个自定义验证属性,以使用正则表达式,其中该表达式存储在全局资源中 public class RegExResourceAttribute : ValidationAttribute, IClientValidatable { private string RegexResource; public RegExResourceAttribute(string regexResource) : base() { RegexResource = regexR

我创建了一个自定义验证属性,以使用正则表达式,其中该表达式存储在全局资源中

 public class RegExResourceAttribute : ValidationAttribute, IClientValidatable
 {
   private string RegexResource;

   public RegExResourceAttribute(string regexResource) : base()
   {
     RegexResource = regexResource;
   }

   protected override ValidationResult IsValid(object value, ValidationContext validationContext)
   {
     if (value != null)
     {
       if (value.GetType() == typeof(string))
       {
         string waarde = value.ToString();
         string resourceWaarde = Global.ResourceManager.GetString(RegexResource);

         if (!string.IsNullOrEmpty(waarde))
         {
           Regex rg = new Regex(resourceWaarde);
           if (rg.IsMatch(waarde))
           {
             return ValidationResult.Success;
           }
           else
           {
             string errorText = Global.ResourceManager.GetString(this.ErrorMessageResourceName);
             errorText = string.Format(errorText);

             return new ValidationResult(errorText);
           }
         }
       }
     }
     return null;
   }

   public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
   {
     ModelClientValidationRule mcvr = new ModelClientValidationRule();
     mcvr.ValidationType = "regexresource";

     string errorText = this.ErrorMessage;
     if (string.IsNullOrEmpty(errorText))
     {
       errorText = Global.ResourceManager.GetString(this.ErrorMessageResourceName);
       errorText = string.Format(errorText);
     }

     string resourceWaarde = Global.ResourceManager.GetString(RegexResource);

     mcvr.ErrorMessage = string.Format(errorText, (metadata.DisplayName == null ? metadata.PropertyName : metadata.DisplayName));
     mcvr.ValidationParameters.Add("regex", resourceWaarde);

     List<ModelClientValidationRule> rules = new List<ModelClientValidationRule>();
     rules.Add(mcvr);

     return rules.AsEnumerable();
   }
 }
在html中,它如下所示:

 var resource = "";

 $.validator.addMethod("rexexresource", function (value, element, params) {

   if (value != null && value != '' && resource != null && resource != '') {
     var patt = new RegExp(resource);
     if (patt.test(value)) {
       return true;
     }
   }
   return false;
 });

 $.validator.unobtrusive.adapters.add('rexexresource', ['value', 'rexex'], function (options) {
   options.rules['rexexresource'] = true; // { targetvalue: options.params.value };
   options.messages['rexexresource'] = options.message;
   resource = options.params.regex;
 });
<input class="input-validation-error form-control invoer" data-val="true" data-val-regexresource="Alleen letters, cijfers, spaties zijn toegestaan. Beginnen met een spatie is niet toegestaan." data-val-regexresource-regex="^(\S(?:[\-0-9a-zA-ZèéíáïëäöüñÈÉÍÁÏËÄÖÜßÑç\s\.\'.?])+)$" data-val-required="Voer uw straatnaam in" id="txtStraat" name="straat" onblur="check_val(this)" oninput="show_label(this)" type="text" value="[][][[]">

因此,正则表达式代码存储在资源中,以便在需要时进行快速编辑,而无需发布项目

c#part可以正常工作,但只有在提交表单时才可以。 javascript部分不工作。我在document.ready中有此代码,并添加了该方法,但当我输入表达式中不包含的字符时,不会验证任何内容

jquery验证脚本文件加载在页面顶部,带有上述javascript代码的javascript文件加载在页面底部

我做错了什么

更新(2021-04-20):

尝试添加一些JavaScript“警报”语句,以确定正在执行的代码以及变量值是否符合预期。这样做可以说明问题所在。一旦问题被纠正,然后注释掉“警告”语句。@JohnH-有人有想法吗?请?我还没有找到解决这个问题的办法。有人能帮我吗?