CalcBuilder Forum

Validate field greater or less than other field

Usuario fdiagou1 2015-09-17 01:20:37

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 2015-09-17 09:44:12

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,

Usuario fdiagou1 2015-09-18 00:06:22

Thanks,
It helped a lot.

Moonsoft support 2015-11-16 17:59:19

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

Moonsoft support 2015-11-16 18:00:11

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

Moonsoft support 2015-11-16 20:40:52

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 2015-11-17 10:22:00

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

Moonsoft support 2015-11-17 10:25:17

Hi,

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

Regards,

Moonsoft support 2015-11-17 10:57:37

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 2015-11-17 11:06:08

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

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

Buy now!
...
Support/development 40 hours

With the peace of mind of having a professional team at your service (20% discount)

Buy now!