Javascript compare 2 numeric fields for form on submit

Hi There,

I’m new to this, but I’m sure this will be easy for some of you veterans!

Scene with two views:
view 91 is a details view
view 88 is a form view

I want to have the form validate that a field from the details view is greater than a field from the form view on click - submit.

$(document).on(‘knack-view-render.view_88’, function(event, view, data) {
var LocationBoxes = $(‘Knack.models.view_91.attributes.field_101_raw’).val()
var RequestBoxes = $(’#view_88-field_108’).val()
$("#view_88 .kn-button").on(“click”, function() {
if (LocationBoxes < RequestBoxes) {
alert (“These are not the droids you are looking for.”);
return false;
}
})
});

Do I need to define the fields as variables or can I call them directly? Am I messing up the syntax somewhere?

*Any help appreciated!

Thanks,
Lauren

Hello Lauren,

Try below
$(document).on(‘knack-view-render.view_88’, function(event, view, data) {

$("#view_88 .kn-button").on(“click”, function() {
var LocationBoxes = Knack.models.view_91.attributes.field_101_raw;
var RequestBoxes = $(’#view_88-field_108’).val();
if (LocationBoxes < RequestBoxes) {
alert (“These are not the droids you are looking for.”);
return false;
}
})
});

Regards,
Sunny Singla
ssingla1985@gmail.com

Hi @Sunny_Singla - Thanks so much for the input. Per your suggestion, I changed the syntax in the variable line for the details field. Unfortunately, it’s still not executing the if statement as I’d hoped.

I’m wondering if the on click for the .kn-button is the best way to tag the event listener or if it should be something else as it’s a submit button type?

Hello Lauren,

Just try the below code. Also, check values in the console. If values are not showing then maybe there is another issue. If working then remove alert

$("#view_88 button").live(“click”,function(){
alert(‘testing’);
var LocationBoxes = Knack.models.view_91.attributes.field_101_raw;

var RequestBoxes = $(’#view_88-field_108’).val();
console.log(LocationBoxes,RequestBoxes);
if (LocationBoxes < RequestBoxes) {

alert (“These are not the droids you are looking for.”);

return false;

}

});

1 Like

Sometimes in javascript, you need to convert $(’#view_88-field_108’).val() type of values to int using parseInt if you want to compare with integer value

1 Like

Also, check the Id of the text field.

I think Id is “field_108” instead of “view_88-field_108”

1 Like