Skip to main content
CalcBuilder logo
CalcBuilder Forum

Validate field greater or less than other field

User fdiagou1

Hi,

I have a form with some number fields in it. I need to validate the field value based on the value of the other field.
For instance, retiring age should be greater than current age.

Is that possible? If yes, how can I do it?

Thanks in advance

Moonsoft support

Hi,
yes, the module includes the jquery validator, so you can extend the default validations adding any custom functions. In order to add a new validation 'this field must be greater than 'number1' field', this is the code you should add to the first javascript tab of the calculator 'executed on loaded page':


CB.validator.addMethod("cb_greaterthan", function(value, element, params) {
if (value=='') return false;
value=getNumber(value);
//Replace here 'number1' for your field name (currentage)

var valueinit=CB("input").val();

valueinit=getNumber(valueinit);
return value>valueinit;

//Replace here the message for the preferred one

}, CB.validator.format("Value must be greater than number1"));

CB.validator.addClassRules({
cb_greaterthan : { cb_greaterthan : true }
});



Just replace 'number1' for your field name (currentage). Now you can add this validation to the second field (retiring age), this is done at the 'field configuration'. Add

cb_greaterthan

to its 'Css class' field.

Hope this helps,

User fdiagou1

Thanks,
It helped a lot.

Moonsoft support

Hi i need that the number 1 in the message up there to be the variable data itself ??

Moonsoft support

Hi i need that the number 1 in the message up there to be the variable data itself ??

Moonsoft support

Hi,

You could add variable to message changing message format and class rules like this:

CB.validator.addMethod("cb_greaterthan", function(value, element, params) {
if (value=='') return false;
value=getNumber(value);
//Replace here 'number1' for your field name (currentage)
var valueinit=CB("input").val();
valueinit=getNumber(valueinit);
return value>valueinit;
//Replace here the message for the preferred one
}, CB.validator.format("Value must be greater than {0}"));

CB.validator.addClassRules({
cb_greaterthan : { cb_greaterthan : function(){return CB("input").val();} }
});



Regards,

Moonsoft support

Thank You. But it doesn't display any messages at all.

Moonsoft support

Hi,

Did you replace 'number1' for your field name (currentage)?

Regards,

Moonsoft support

yes i did. It is named "deposit" in my case.

CB.validator.addMethod("cb_greaterthan", function(value, element, params) {
if (value=='') return false;
value=getNumber(value);
var valueinit=CB("input").val();
valueinit=getNumber(valueinit);
return value>=valueinit;
}, CB.validator.format("Value must be greater than {0}"));

CB.validator.addClassRules({
cb_greaterthan : { cb_greaterthan : function(){return CB("input").val();} }
});

Moonsoft support

Hi,

With this code and setting 'cb_greaterthan' class in the other field, should be work. Please export your calculator and place a private helpdesk ticket and we'll take a look.

Regards,

...
Support/development 10 hours

Get a bigger amount of hours for more complex tasks and get a 10% discount

Buy now!
...
Support/development

Perfect for small code changes or to correct any bug at your site

Buy now!