1
$("#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??

3 Answers 3

1

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());
}
Sign up to request clarification or add additional context in comments.

3 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
1

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.

4 Comments

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."
1

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());
}

Comments

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.