4

How to get value of var total in message,

and also i tried declare inside function but it gives undefined variable

var total = '';
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    var fund_old = $("#current_fund").val();
    var fund_new = $("#new_fund").val();

    total = parseFloat(9999999999.999999) - parseFloat(fund_old);

    if (parseFloat(fund_new) <= parseFloat(total)) {
        return true;
    } else {
        return false;
    }
    return true;
}, 'sry sktiman' + total + 'is remaining value');

In result i am getting blank value of total

4
  • 1
    Have you checked that $("#current_fund").val() gives you any/correct value? Commented Mar 4, 2014 at 5:51
  • @Akshay it is already set Commented Mar 4, 2014 at 5:53
  • Please show the HTML markup and the call to .validate(). Commented Mar 7, 2014 at 19:43
  • 2
    Your whole approach to using custom validators with addMethod is deeply flawed. These validators are supposed to be reusable, but yours will always do the same, comparing the values of #current_fund and #new_fund. I'd suggest to start over again, read the documentation and have a look at some of the demos. Commented Mar 7, 2014 at 19:54

5 Answers 5

7

According to the documentation for jQuery.validator.addMethod() you can use jQuery.validator.format() which generates a function that receives the arguments to the validation method and returns a string message, so creating a function that ignores any arguments and returns a string should work:

var total = '';
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    var fund_old = $("#current_fund").val();
    var fund_new = $("#new_fund").val();

    total = parseFloat(9999999999.999999) - parseFloat(fund_old);

    if (parseFloat(fund_new) <= parseFloat(total)) {
        return true;
    } else {
        return false;
    }
    return true;
}, function() {return 'sry sktiman' + total + 'is remaining value'});

EDIT

The fiddle for this solution can be found here (thanks to Sparky for providing the code).

Sign up to request clarification or add additional context in comments.

1 Comment

@Sparky Yes, it does, you just have to declare the total var in the global scope, as the OP did. See this fiddle.
6

The optional parameters can be used inside the message.

The third argument, the optional parameters, (you call it arg) can be represented within your code as arg[0], arg[1], etc.

Then the corresponding values can be used in your message as {0}, {1}, etc.

Do your calculation external to .addMethod() and pass the value in using the arg argument.

$.validator.addMethod("valueNotEquals", function(value, element, arg){

    var fund_old= $("#current_fund").val();
    var fund_new =$("#new_fund").val();

    // do this calculation where the rule is declared. See 'validate()'
    //total =  parseFloat(9999999999.999999) - parseFloat(fund_old);

    if(parseFloat(fund_new) <= parseFloat(total)){
        return true;
    } else {
        return false;
    }
    return true;
},'sry sktiman {0} is remaining value');

Since you didn't show your .validate() or the HTML markup, maybe something like this...

$('#myform').validate({
    rules: {
        fund_old: {
            // your rules
        },
        fund_new: {
            valueNotEquals: function() {
                return ($parseFloat(9999999999.999999) - parseFloat($("#current_fund").val()));
            }
        }
    }
});

Crude demo: http://jsfiddle.net/eZm7x/

1 Comment

I prefer this parameter way. Nice answer.
1

Try,

var total = '';
$.validator.addMethod("valueNotEquals", function(value, element, arg){
  var fund_old= $("#current_fund").val();
  var fund_new =$("#new_fund").val();

 total =  parseFloat(9999999999.999999) - parseFloat(fund_old);

      if(parseFloat(fund_new) <= parseFloat(total)){
        return true;
      }else{
        return false;
      }
 return true;
 },'sry sktiman'+ (parseFloat(9999999999.999999) - parseFloat($("#current_fund").val())) + 'is remaining value');

Comments

1

In addMethod Documentation says: "Add a custom validation method. It must consist of a name (must be a legal javascript identifier), a javascript based function and a default string message."

So, you are adding a custom validation function to plugin, and NOT executing that function.

Try to set a field with your custom validation and fill that field to run your custom function. Just AFTER that check total var value.

Comments

0
+100

Just add this line function() {return 'sry sktiman' + total + 'is remaining value'} as your second argument, you can easily use this variable

var total = ''; 
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
//Your default code here
}, function() {return 'sry sktiman' + total + 'is remaining value'});

1 Comment

can you explain more it ?

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.