Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:24:20+00:00 2026-05-27T03:24:20+00:00

I try to use This Way to fire a custom validator in client side

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:24:21+00:00Added an answer on May 27, 2026 at 3:24 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This article on MSDN states that you can use as many try catch blocks
I know, that I am not the first one to try to use Cocoa
I have a DateTime field in my Model. If I try to use this
I try to use this code for toggling text on the link: var showText=<span>Open</span>
I try to use this mapping : @Entity @Table(name=ecc.\RATE\) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=DISCRIMINATOR, discriminatorType= DiscriminatorType.STRING) public
I get this error when I try to use time_ago_in_words : Comparison of String
Right now we just use something like this stopWatch.Start(); try { method(); } finally
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
try { OpenConnection(); RowsAffected = cmd.ExecuteNonQuery(); CloseConnection(true); //should I use this function call here
currently I have this client code in my PHP MVC web app: try {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.