Model Property
[Display(Name = "SchoolName CustomValidator(ValidName='MySchool')")]
[ExactString(ValidName = "MySchool", ErrorMessage = "The Input Name must exactly equal by Valid Name")]
public string SchoolName { get; set; }
Custom Validation class
public class ExactStringAttribute : ValidationAttribute ,IClientValidatable
{
public string ValidName { get; set; }
public override bool IsValid(object value)
{
if (value != null)
if (value.ToString() == ValidName)
return true;
return false;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationParameters.Add("validname", ValidName);
rule.ValidationType = "exactstring";
yield return rule;
}
}
[Display(Name = "SchoolName CustomValidator(ValidName='MySchool')")]
[ExactString(ValidName = "MySchool", ErrorMessage = "The Input Name must exactly equal by Valid Name")]
public string SchoolName { get; set; }
Custom Validation class
public class ExactStringAttribute : ValidationAttribute ,IClientValidatable
{
public string ValidName { get; set; }
public override bool IsValid(object value)
{
if (value != null)
if (value.ToString() == ValidName)
return true;
return false;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationParameters.Add("validname", ValidName);
rule.ValidationType = "exactstring";
yield return rule;
}
}
Add Jquery validate function
<!-- Start : Register Exact String Validation -->
<script>
if ($.validator && $.validator.unobtrusive) {
$.validator.unobtrusive.adapters.addSingleVal("exactstring", "validname");
$.validator.addMethod("exactstring", function (value, element, validname) {
if (value) {
if (value == validname) {
return true; /* success */
}
}
return false;
});
}
</script>
<!-- End : Register Exact String Validation -->
Comments
Post a Comment