2016-01-22 25 views
0

在我的模型中,我有一個IP地址的字符串屬性。 這可以是v4或v6的IP地址。 我想驗證輸入等等,與網絡上的一些研究,我創建了這個類:MVC自定義屬性的IP地址不工作

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
using System.Web.Mvc; 

namespace ISR.CustomValidators 
{ 
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
    sealed public class IsIPaddress : ValidationAttribute, IClientValidatable 
    { 
    private const string DefaultErrorMessage = "{0} is not a valid IP address."; 

    public IsIPaddress() : base(DefaultErrorMessage) 
    { 
    } 

    public override string FormatErrorMessage(string name) 
    { 
     return string.Format(ErrorMessageString, name); 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     if (value != null) 
     { 
     Regex rgIP4 = new Regex(@"^(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|25[0-5]|2[0-4]\d)$"); 
     Regex rgIP6 = new Regex(@"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"); 

     if (rgIP4.IsMatch(value.ToString())) 
     { 
      return ValidationResult.Success; 
     } 
     if (rgIP6.IsMatch(value.ToString())) 
     { 
      return ValidationResult.Success; 
     } 
     } 
     return null; 
    } 

    IEnumerable<ModelClientValidationRule> IClientValidatable.GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     return new[] { new ModelClientValidationIsIPaddress(FormatErrorMessage(metadata.GetDisplayName()))  }; 
    } 
    } 

    public class ModelClientValidationIsIPaddress : ModelClientValidationRule 
    { 
    public ModelClientValidationIsIPaddress(string errorMessage) 
    { 
     ErrorMessage = errorMessage; 
     ValidationType = "isipaddress"; 
    } 
    } 
} 

我還創建了這個腳本文件的客戶端驗證:

(function ($) { 
    jQuery.validator.addMethod("isipaddress", function (value, element) { 
    //if (this.optional(element)) return true; 

    return true; 
    }); 

    jQuery.validator.unobtrusive.adapters.addSingleVal("isipaddress"); 
}(jQuery)); 

型號:

[IsIPaddress] 
public string Network { get; set; } 

我檢查了網頁,腳本文件在那裏,也是Jquery驗證腳本,都是按照正確的順序。 但它不工作。

我哪裏錯了?我做錯了什麼? 我需要做些什麼才能使這個工作? 有沒有人有如何創建自定義驗證屬性的好例子?

[編輯]

,因爲我用了大量的Ajax調用我有一個功能叫我以後每次Ajax調用重新初始化驗證和我放在AddMethod代碼在那裏了。

現在我看到一個SPAN被添加到驗證消息的HTML中,但它是空的。 因此,不知何故默認的錯誤信息沒有顯示。 我爲此忘記了什麼嗎?

+0

它不起作用?它沒有做任何事情嗎?它是否強迫你穿越溪流,從而導致你身體中的每個分子以光速爆炸? – krillgar

+0

它沒有做任何事情。 – Eric

回答

0

我解決了它。

在validator.AddMethod ...我沒有任何代碼來執行檢查。 我認爲這將從屬性代碼自動創建。

所以,我把它改成這樣:

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

     var regexV4 = new RegExp('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'); 
     var regexV6 = new RegExp('^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$'); 

     if (regexV4.test(value)) { return true; } 
     if (regexV6.test(value)) { return true;} 

     return false; 
    }); 

    $.validator.unobtrusive.adapters.addBool("isipaddress"); 

現在是工作的罰款。

1

據我記得,不顯眼的jQuery驗證插件初始化automaticaly和問題是.addMethod調用是在初始化後,它是後來。您應該嘗試在驗證初始化之前添加方法或清除表單驗證,再次添加方法和初始化驗證。

+0

你能告訴我這是怎麼完成的嗎? – Eric