1

I try to use This Way to fire a custom validator in client side but that not work correctly:

this is my model and my validator:

public class ExactStringAttribute : ValidationAttribute {

    public string ValidName { get; set; }

    public override bool IsValid(object value) {

        if (value.ToString() == ValidName)
            return true;
        else
            return false;
    }
}

public class Teacher {

    public long Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public int Age { get; set; }
    [Display(Name = "SchoolName CustomValidator(ValidName='MySchool')")]
    [ExactString(ValidName = "MySchool", ErrorMessage = "The Input Name must
       exactly equal by Valid Name")]
    public string SchoolName { get; set; }
}

this is my script:

<script type="text/javascript">
Sys.Mvc.ValidatorRegistry.validators["exactstring"] = function (rule) {
    // initialization code can go here.
    var validname = rule.ValidationParameters["validname"];

    // we return the function that actually does the validation 
    return function (value, context) {
        if (value == validname) {
            return true; /* success */
        }
        return rule.ErrorMessage;
    };
};
</script>

and a part of Global.asax:

protected void Application_Start() {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(ExactStringAttribute),
typeof(ExactStringValidator));
    }




    public class ExactStringValidator : 
DataAnnotationsModelValidator<ExactStringAttribute> {

        string validName;
        string errorMessage;

        public ExactStringValidator(ModelMetadata metadata,
 ControllerContext context
          , ExactStringAttribute attribute)
            : base(metadata, context, attribute) {
            validName = attribute.ValidName;

        }

        public override IEnumerable<ModelClientValidationRule>
         GetClientValidationRules() {
            var rule = new ModelClientValidationRule {
                ErrorMessage = errorMessage,

                //Must be lowercase
                ValidationType = "exactstring"

            };

            //Must be lowercase
            rule.ValidationParameters.Add("validname", validName);

            return new[] { rule };
        }
    }

And Also I Add Script in _Layout Page:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript">
</script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript">    
</script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript">
</script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" 
type="text/javascript"></script>

A part of Controller:

[HandleError]
public class TeacherController : Controller {
......
}

When I add Html.EnableClientValidation(); in view page that represent an error about argument but in web.config file the following setting added:

<appSettings>
    <add key="webpages:Version" value="1.0.0.0"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

But unfortunately the validator didn't fire in client side, does any one know abut this?

1 Answer 1

2

If you look at the methods used on this answer:

MVC custom validation: compare two dates

you will see the changes you should make to your validator components, as well as what you must add.

Sign up to request clarification or add additional context in comments.

1 Comment

That not work for me, I don't know where is the problem, I ask it here: stackoverflow.com/questions/8284207/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.