How to update jQuery Validate message dynamically

jquery validate custom rule
jquery validate onfocusout
jquery validate errorplacement
jquery validation example
jquery validate depends
jquery validate add rule
jquery validate on submit
jquery custom form validation
$("#frmNewContracts").validate({

      errorClass   : "state-error",
      validClass   : "state-success",
      errorElement : "span",

      rules: {

            txtusername  :  { required    : true,
                              rangelength : [5, 18],
                              remote      : { url  : "./_aQ8/auxphp/ajxcontrol.php", type : "get" }
                            }
      },

      messages: {

            txtusername  :  { required    : "User-name is required",
                              rangelength : "User-name must be between 5 - 18 characters",
                              remote      : $.validator.format("User-name :  <strong>{0}</strong> is already taken please try another")
                            }
      },
        errorLabelContainer: "#joinerrgroup",
        wrapper: "li"               
});

Hi, As the question suggests i have a form validating with jQ Validate plugin the code provided above is a snippet I've isolated AND tried alone with my html form and the problem is the same.

The Problem Is : The username should update dynamically (or so i thought) to display the users entered username when it already exists in the DB table - all is working ok but when testing and trying several duplicate Usernames, deliberately to check the functionality, the message displays correctly but the username continues to show the FIRST duplicate username entered. It does not update to show the duplicate name currently in the username form field. Is this normal behavior i do i need to do something else??

You can get the username with jQuery and then use in the format method.

remote: function() {
  return jQuery.format("User-name : <strong>{0}</strong> is already taken please try another", $("#username").val());
}

Validate: need dynamic message on failed validation, I need to include the contents of a field in a validation error message. I tried using a closure around the addMethod call as demonstrated in the  How to validate dynamically generated input fields with jQuery Validation plugin ?? Himalay Majkhand javascript , jQuery , jQuery Dynamic field validation , jQuery Validator one response As we know jQuery validation plugin is the most commonly used client side form validation library which provides plenty of customization with an ease to save

You do not need the messages option for remote.

As per the docs, if you echo or send a JSON encoded string from your server-side code, validation fails and this string becomes the validation message.

jQuery Validation Plugin – Dynamically change validator message , UPDATE: Please read Guu's comment below for a far better solution to this problem! I've been using the jQuery Validation Plugin and needed  The problem. A few months ago I built a form for a client that had a huge amount of input elements dynamically added to the dom depending on what the end user had chosen, this was great and worked until the client asked, 'Hey, can we make them mandatory?', Naturally I said yes and set myself the task of finding out how I could make my C# MVC Form setup validate these inputs, the trick was in

I was running into this error and the accepted answer was only working twice as Flim Flam noted in the comments. For me the fix was to add a parameter (I called it "element") to the function that returns a string.

For example:

remote: function(element) {
  return jQuery.format("User-name : <strong>{0}</strong> is already taken please try another", $("#username").val());
}

How to validate dynamically generated input fields with jQuery , As we know jQuery validation plugin is the most commonly used client side form validation library which provides plenty of customization with  Since we're already using JQuery, we can let page designers add custom messages to the markup rather than the code: <input data-msg-required="my message"

Documentation, You need to place error messages in the DOM and show and hide them you can use to trigger validation programmatically or change the contents of the form. The problem is that the jQuery Validate plugin will not work unless each input element contains a unique name. Simply modify your code to ensure that a new name is created for each new element. EDIT: Indexed arrays will work fine with this plugin. Just increment the index when you create the new element.

Reference documentation, An error message displays a hint for the user about invalid elements, and what is wrong. There are four ways to provide error messages. Via the title attribute of the​  However, there is one final issue I hope you can help me with. I cannot seem to get the message inside the tooltip to update itself dynamically. If you click submit, the message on the first field is "required". However, as soon as one letter is entered in the first field, the message should change to "please enter at least 5 characters".

dynamic forms demo, jQuery Validation Plugin Demo. <tr> <td> <label>{0}. Item</label> </td> <td class​='type'> <select name="item-type-{0}"> <option value="">Select</option>  jQuery Forum Move this topic Forum : Getting Started Using jQuery Using jQuery Plugins Using jQuery UI Developing jQuery Core Developing jQuery Plugins Developing jQuery UI QUnit and Testing About the jQuery Forum jQuery Conferences jQuery Mobile Developing jQuery Mobile

Comments
  • Thanks for your answer But now it only works the first TWO times testing but when a Third duplicate name is entered it still show the Second duplicate name in the message. Tried it multiple times and same result. So a little closer but not perfect. Although the odds that someone will enter 3 duplicate names are very low its still an annoying issue.
  • Yes, all my ids are unique, however i think jQuery.validate uses the elements name attribute - either way they too are unique.
  • Update : i've marked yours as the correct answer, since further testing i noticed when successive duplicate names were Typed In as apposed to what i was doing (cut 'n pasting' to save time) the validation worked as you stated. Since most people won't be pasting names in - i believe this was the issue and your solution is indeed correct - thanks for your help. cheers
  • My script isn't failing since when i type in a unique username it functions correctly and as per my response below to 'Tsvetomir Nikolov' - when i type in known duplicate names for testing purposes it identifies the first two correctly then the third time it continues to show the second duplicate name and no longer shows the current duplicate name entered
  • @FlimFlam , yet apparently there's something very wrong with your methodology since it always fails on the third attempt. My answer shows you a more robust and standard method.
  • I can't see where within the docs you got your information - the docs show a technique to access the returns result. The fact that the docs doesn't show the message and format() doesn't mean it cant be used. The sparse examples in the official docs (only two full examples) are constantly negatively commented upon in various SO thread. The results i'm getting point to an issue elsewhere.
  • @FlimFlam, as per this page: "The serverside response must be a JSON string that must be "true" for valid elements, and can be "false", undefined, or null for invalid elements, using the default error message. If the serverside response is a string, eg. "That name is already taken, try peter123 instead", this string will be displayed as a custom error message in place of the default."