你可能希望客户和服务器方面的验证。 落实你自己的成果也许是好的。 我也做了类似的事情,我希望确保一个领域与另一个领域具有同等价值。 不过,我并没有从零开始。 我使用了微软Simon J Ince的一些代码。 他有here。 基本来说,他有一个有条件补偿的基础(根据另一个领域的价值验证)。 我继承了他有条件的AttributeBase,并实施了IClientValidatable接口(这就是,你如何把java的手稿送到附属地的浏览器)。
/// <summary>
/// A validator for ensuring that the value of this field does NOT equal the value of another field.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class NotEqual:ConditionalAttributeBase, IClientValidatable
{
public string DependentProperty { get; set; }
/// <summary>
/// Returns client validation rules for NotEqual
/// </summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <returns>
/// The client validation rules for NotEqual.
/// </returns>
IEnumerable<ModelClientValidationRule> IClientValidatable.GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "notequal",
};
string depProp = BuildDependentPropertyId(metadata, context as ViewContext);
rule.ValidationParameters.Add("dependentproperty", depProp);
yield return rule;
}
/// <summary>
/// Builds the dependent property id.
/// </summary>
/// <param name="metadata">The metadata.</param>
/// <param name="viewContext">The view context.</param>
/// <returns></returns>
protected string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
{
return QualifyFieldId(metadata, DependentProperty, viewContext);
}
/// <summary>
/// Validates that the value does not equal the value of the dependent value if the dependent value is not null
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
/// <returns>
/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
/// </returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// check if the current value matches the target value
if (value != null && GetDependentFieldValue(DependentProperty, validationContext).ToString() == value.ToString())
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
/// <summary>
/// Initializes a new instance of the <see cref="NotEqual"/> class.
/// </summary>
/// <param name="dependentProperty">The dependent property.</param>
public NotEqual(string dependentProperty)
{
DependentProperty = dependentProperty;
}
}$
随后,我打着 j子:
(function ($) {
$.validator.addMethod( notequal ,
function (value, element, parameters) {
var id = # + parameters[ dependentproperty ];
var depControl = $(id);
var control = $(element);
if (control.val() === depControl.val())
return "";
return true;
}
);
$.validator.unobtrusive.adapters.add(
notequal ,
[ dependentproperty ],
function (options) {
options.rules[ notequal ] = {
dependentproperty: options.params[ dependentproperty ]
};
options.messages[ notequal ] = options.message;
}
);
$.validator.addMethod( notequaltwo ,
function (value, element, parameters) {
var id = # + parameters[ dependentproperty ];
var depControl = $(id);
var control = $(element);
if (control.val() === depControl.val())
return "";
return true;
}
);
$.validator.unobtrusive.adapters.add(
notequaltwo ,
[ dependentproperty ],
function (options) {
options.rules[ notequaltwo ] = {
dependentproperty: options.params[ dependentproperty ]
};
options.messages[ notequaltwo ] = options.message;
}
);
})(jQuery);$
希望你们能够看到你如何调整我的准则,以做你想要做的事情,但基本上,你们必须改变这些类型(如果这些类型不改变,就没有做什么,也没做你应该做的其他人)。