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

  • SEARCH
  • Home
  • 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 7183013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:53:34+00:00 2026-05-28T17:53:34+00:00

I am looking to get some custom validation attribute in MVC 3 to fire

  • 0

I am looking to get some custom validation attribute in MVC 3 to fire only when I submit and not when the input loses focus.

Here is my custom validation attribute.

 #region Password Match Validation Attribute
    public sealed class PasswordPresent : ValidationAttribute, IClientValidatable
    {
        private const string _defaultErrorMessage = "Current Password no present";
        private string _basePropertyName;
        public PasswordPresent(string basePropertyName)
            : base(_defaultErrorMessage)
        {
            _basePropertyName = basePropertyName;
        }
        public override string FormatErrorMessage(string name)
        {
            return string.Format(_defaultErrorMessage, name, _basePropertyName);
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var basePropertyInfo = validationContext.ObjectType.GetProperty(_basePropertyName);
            var oldPassword =  (string)basePropertyInfo.GetValue(validationContext.ObjectInstance, null);
            var newPassword = (string)value;

            if (!string.IsNullOrEmpty(newPassword) && string.IsNullOrEmpty(oldPassword))
            {
                var message = FormatErrorMessage(validationContext.DisplayName);
                return new ValidationResult(message);
            }
            return null;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules
            (ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());

            //This string identifies which Javascript function to be executed to validate this 
            rule.ValidationType = "passwordpresent";
            rule.ValidationParameters.Add("otherfield", _basePropertyName);
            yield return rule;
        }
    }
    #endregion

Here is the .js file for clientside validation

jQuery.validator.addMethod("passwordmatch", function (value, element, param) {

    if (value == null || value.length == 0) {
        return true;
    }
    return value == $(param).val(); ;

});


jQuery.validator.unobtrusive.adapters.add("passwordmatch", ["otherfield"], function (options) {
    options.rules["passwordmatch"] = "#" + options.params.otherfield;
    options.messages["passwordmatch"] = options.message;
});

Here is my Model

public class UserSettingsModel
    {

        [Display(Name = "Old Password")]
        [PasswordPresent("NewPassword",ErrorMessage = "Please enter in a New Password")]
        public string CurrentPassword { get; set; }

        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [PasswordsMatch("NewPassword",ErrorMessage = "Confirm password and New Password do not match")]
        public string ConfirmPassword { get; set; }

    }

And here is my View.

@model Data.Models.UserSettingsModel
   <div id="UserSettings">
 @using (Ajax.BeginForm("UpdateSettings", "AccountManagement", new AjaxOptions() {UpdateTargetId = "UserSettings", HttpMethod = "Post" }))
 {
        @Html.ValidationSummary(true)

        <div class="tabs" id="userTab3">
            <fieldset>
                <div>
                    <h3>
                        Change Password</h3>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.CurrentPassword)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.CurrentPassword)
                        @Html.ValidationMessageFor(model => model.CurrentPassword)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.NewPassword)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.NewPassword)
                        @Html.ValidationMessageFor(model => model.NewPassword)
                    </div>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.ConfirmPassword)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.ConfirmPassword)
                        @Html.ValidationMessageFor(model => model.ConfirmPassword)
                    </div>
                </div>

                <p>
                    <input type="submit" value="Save & Continue" name="btnsubmit" />
                </p>
            </fieldset>
        </div>
 }
 </div>
  • 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-28T17:53:35+00:00Added an answer on May 28, 2026 at 5:53 pm

    Assuming you’re using the jQuery Validate plugin, you’ll need to modify it to bind to only the submit button/form submission. Check this thread out:

    jQuery validate rule ONLY AT SUBMIT

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

Sidebar

Related Questions

still a bit of a n00b on SharpSVN, I'm looking to get some simple
Where can I get some decent looking free ASP.Net or CSS themes?
I was looking at this article from 2005 and wanted to get some thoughts
I'm looking to make a website that will probably get some heavy, repetitive traffic.
I'm looking to parse some CXML in PHP...basically all I'm looking to get the
I'm looking at some open source Java projects to get into Java and notice
Whenever there is some custom validation to do or any fiddling with ModelState I
I'm wondering if I can get some custom controls in winforms .NET? Is there
I'm working on a custom validation framework for my WPF/C# application. What I'm looking
I'm looking for a way to use custom ASP.NET validators to validate input, without

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.