Preventing form submit if value not met

Hi,

I have found:

// Use on the Knack Standard Theme
$(document).on('knack-view-render.view_1', function(event, view, data) {
  $("#view_1 .kn-button").on("click", function() {
    // If this value in the form doesn't equal "SpecificValue" then prevent the form from submitting
    if ($("#view_1-field_1").val() != "SpecificValue") {
     alert ("These are not the droids you are looking for.");
     return false;
    }
  })
});

I can get the alert to show but I am unable to get a positive match allowing the form to submit.

Has anyone else had the same problem please and know how to fix?

I am using the knack standard theme and have changed to the correct view and field values so not sure why it is not working correctly.

Thank you!

Paul

Have you tried using field-level validation rule for this? I use these validation rules to ensure that fields are not empty, that the values in them make sense, and so on. If the validation fails when user clicks Submit on a form, they’ll get an error message that I wrote for this error case. Otherwise, if validation succeeds, the record gets submitted.

Forgive me if I’m completely misunderstanding the problem. I’m new here myself. :slight_smile:

Thanks William. Yes I could do that so thank you. I would like to get this javascript working if I can as it would be a tidy solution for me. Thank again, Paul

I suppose simply adding something like this wouldn’t do the trick?

else
   return true

No the issue is the “SpecificValue” is not being read correctly. So if the value matches or not makes not difference for me, the alert is always show. Conversely if I switch != (does not equal) to == (equals) the alert is never shown regardless of the “SpecificValue”. I have tried integers and alternative fields but I can’t get it to work. I think I will message support see if they will help. Thanks again!

Did you ever get an answer on this/ I’m seeing the same issue.

Sadly not - Support got back to me, but we only established the knack code works for integers but not text. I tried various modifications, but I could not get it to work. Shame it would be handy. If anyone does know how please post!

Paul, after hacking around yesterday I got something working that you might be able to use. In my case, I couldn’t use field validation because I wanted to test that the content of the STATE field in an ADDRESS was entered and valid. Knack wont give access to the contents of the subfields for validation, so its over to javascript…

Now, I am not a developer- this is cobbled together from other peoples’ ideas and not very elegant. But it works, so I don’t care.

I create a variable called SCREENSTATE which scrapes the content of the STATE subfield in field 56 (ADDRESS) and then compares the uppercase of SCREENSTATE to an array STATES that contains all the valid entries for Australia, to derive a boolean outcome VALIDSTATE. If its not a match then VALIDSTATE is FALSE, so I post an error message and stop. As this happens before any other Knack submit validations, it becomes the first hurdle the user has to pass before they can submit the form.

$(document).on(‘knack-view-render.view_29’, function(event, view, data) {
$("#view_29 .kn-button").on(“click”, function() {
const states= [“VIC”, “NSW”, “QLD”, “TAS”, “SA”, “WA”, “NT”, “ACT”];
var screenstate = $(’#kn-input-field_56 #state’).val();
var validstate = states.includes(screenstate.toUpperCase());
if (validstate === false) {
$(“form .is-error”).remove(); // remove error message if exist
$(“form”).prepend(‘

STATE is required.

’);
return false;
}
})
});

If I understand your use case, you are trying to compare two on screen fields? Maybe you could create 2 variables being each of the two fields you care about and compare them to get the boolean outcome?
In your case, you would drop the #state in line 4 - that is to get at the state subfield in the address. If it was a normal text field it would just say: var screenstate = $(’#kn-input-field_56’).val();

Hope this helps in some way.

Thanks Leigh,

I will give it a go. Thank You!