0

I have a custom validation rule, so I am using to check it using addMethod. On rules, I am sending some JSON formatted string to be fetched on the addMethod.

$.validator.addMethod("chkduplicate", function(value, element, params) {
    console.log(params);
}, "This field value already exists.");

Rules should look like:

rules: {
  meter_id: {
    required: true,
    digits: true,
    chkduplicate: '{ "table_name": "users", "fld_name": "user_id"}'
  }
}

here chkduplicate is the parameter I am sending to addMethod, but when I try to console the params, it always returns true, but it should be {"table_name": "users", "fld_name": "user_id"}'

I am using this plugin

9
  • 1
    why are you trying to JSON.parse params? It should already be an object. I see no JSON here. JSON is a textual representation of object data...but your rules above looks like an actual JavaScript object literal, to me. And in any case unless you are getting the rules data from somewhere else (e.g. fetching it from a remote server via AJAX) then there would be no reason to need JSON. Commented Feb 1, 2019 at 14:06
  • ok, I removed JSON.parse, however, there is no issue with this, I am saying params is the problem Commented Feb 1, 2019 at 14:08
  • Your addMethod/validation call looks like what? My guess is the issue is there and not in the code you showed us. Commented Feb 1, 2019 at 14:08
  • 1
    Just write chkduplicate: { "table_name": "users", "fld_name": "user_id"} without the single quotes...there's no need to make it a string as far as I can see Commented Feb 1, 2019 at 14:09
  • P.S. I'm guessing this is some kind of custom validation plugin...so it would really help if you tell us what plugin you're using, then we can understand how it's supposed to actually work. Commented Feb 1, 2019 at 14:10

1 Answer 1

2

params Type: Object parameters specified for the method, e.g. for min: 5, the parameter is 5, for range: [1, 5] it's [1, 5]

Yours is string; try this

rules: {
  meter_id: {
    required: true,
    digits: true,
    chkduplicate: { "table_name": "users", "fld_name": "user_id"}
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@JothiKannan Does it still print true?
@JothiKannan see my comment above on the main thread. In theory your code should be fine (and so should the code in this answer). I made a demo to prove it. So if you still have a problem, you'll need to add enough of your real code into the question such that we can reproduce the issue. And/or you could update the demo page with your code, such that it crashes. Then we can see what to fix. At the moment, you haven't given us the key piece of information to make it behave the way you're describing.
@JothiKannan Please see here jsfiddle.net/dubxv0eL I have made an example Edit to make it look like your code

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.